日志修改动作参数为service,流量统计client改为由连接池工具clientUtil中获取
This commit is contained in:
@@ -16,7 +16,6 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.ws.rs.client.WebTarget;
|
||||
@@ -25,21 +24,16 @@ import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPatch;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.utils.URIBuilder;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
@@ -64,52 +58,43 @@ public class HttpClientUtil {
|
||||
* @throws IOException
|
||||
* @throws ClientProtocolException
|
||||
*/
|
||||
public static String get(String url) throws Exception{
|
||||
|
||||
CloseableHttpClient httpclient =null;
|
||||
//请求结果
|
||||
CloseableHttpResponse response = null;
|
||||
String content ="";
|
||||
logger.info("流量统计数据请求路径:"+url);
|
||||
public static String get(String destUrl) throws Exception{
|
||||
//执行get方法
|
||||
String result = null;
|
||||
Response response=null;
|
||||
String url = "";
|
||||
try {
|
||||
//实例化get方法
|
||||
HttpGet httpget = new HttpGet(url);
|
||||
//实例化httpclient
|
||||
httpclient = HttpClients.createDefault();
|
||||
requestConfig = RequestConfig.custom()
|
||||
.setSocketTimeout( Constants.HTTP_SOCKET_TIMEOUT)
|
||||
.setConnectTimeout( Constants.HTTP_CONNECT_TIMEOUT)
|
||||
.setConnectionRequestTimeout( Constants.HTTP_CONNECT_REQUEST_TIMEOUT)
|
||||
.build();
|
||||
httpclient = HttpClients.custom().setDefaultRequestConfig(requestConfig)
|
||||
.setRetryHandler(new DefaultHttpRequestRetryHandler( Constants.HTTP_CONNECT_RETRY_TIMES, false))
|
||||
.build();
|
||||
response = httpclient.execute(httpget);
|
||||
if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
|
||||
content = EntityUtils.toString(response.getEntity(),"utf-8");
|
||||
logger.info("获取流量统计数据成功,相应内容如下: " + content);
|
||||
}else {
|
||||
logger.error("获取消息失败,相应内容如下: " + content);
|
||||
throw new ConnectException("流量统计服务接口连接错误"+content);
|
||||
URIBuilder uriBuilder = new URIBuilder(destUrl);
|
||||
System.err.println(uriBuilder);
|
||||
url=uriBuilder.toString();
|
||||
logger.info("流量统计数据请求路径:"+url);
|
||||
//创建连接
|
||||
WebTarget wt = ClientUtil.getWebTarger(url);
|
||||
logger.info("getMsg url:"+url);
|
||||
//获取响应结果
|
||||
Builder header = wt.request(MediaType.APPLICATION_JSON).header("Content-Type", MediaType.APPLICATION_JSON);
|
||||
response= header.get();
|
||||
int status = response.getStatus();
|
||||
if (status == HttpStatus.SC_OK) {
|
||||
result= response.readEntity(String.class);
|
||||
//调用处理数据方法
|
||||
logger.info("获取消息成功,相应内容如下: " + result);
|
||||
} else {
|
||||
logger.error("获取消息失败,相应内容如下: " + result);
|
||||
throw new ConnectException("流量统计服务接口连接错误"+result);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
throw e;
|
||||
}finally {
|
||||
if (response != null) {
|
||||
try {
|
||||
response.close();
|
||||
} catch (IOException e) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
try {
|
||||
httpclient.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return content;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,7 +105,7 @@ public class HttpClientUtil {
|
||||
* @throws IOException
|
||||
* @throws ClientProtocolException
|
||||
*/
|
||||
public String post(String url,Map<String, String> params) throws ClientProtocolException, IOException{
|
||||
/* public String post(String url,Map<String, String> params) throws ClientProtocolException, IOException{
|
||||
//实例化httpClient
|
||||
CloseableHttpClient httpclient =null;
|
||||
//结果
|
||||
@@ -173,7 +158,7 @@ public class HttpClientUtil {
|
||||
}
|
||||
}
|
||||
return content;
|
||||
}
|
||||
} */
|
||||
/**
|
||||
* 处理patch请求.
|
||||
* @param url 请求路径
|
||||
@@ -199,13 +184,9 @@ public class HttpClientUtil {
|
||||
httpclient = HttpClients.createDefault();
|
||||
//实例化patch方法
|
||||
HttpPatch httpPatch = new HttpPatch(url);
|
||||
|
||||
httpPatch.setHeader("Content-type", "application/json");
|
||||
|
||||
httpPatch.setHeader("Charset", "utf-8");
|
||||
|
||||
httpPatch.setHeader("Accept", "application/json");
|
||||
|
||||
httpPatch.setHeader("Accept-Charset", "utf-8");
|
||||
//提交的参数
|
||||
// UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(nvps, "UTF-8");
|
||||
@@ -236,10 +217,12 @@ public class HttpClientUtil {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
try {
|
||||
httpclient.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
if (httpclient != null) {
|
||||
try {
|
||||
httpclient.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return content;
|
||||
|
||||
Reference in New Issue
Block a user