更新流量统计浏览器,操作系统,网站码表,httpclient工具设置超时配置参数
This commit is contained in:
@@ -10,8 +10,6 @@ package com.nis.util.httpclient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ConnectException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
@@ -36,7 +34,6 @@ 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.conn.ConnectTimeoutException;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
|
||||
@@ -45,14 +42,12 @@ import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.web.servlet.support.RequestContext;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.google.gson.Gson;
|
||||
import com.nis.exceptions.MaatConvertException;
|
||||
import com.nis.util.Constants;
|
||||
import com.nis.util.Encodes;
|
||||
import com.nis.util.StringUtil;
|
||||
|
||||
/**
|
||||
* @ClassName: HttpClientBean.java
|
||||
* @Description: TODO
|
||||
@@ -71,22 +66,46 @@ public class HttpClientUtil {
|
||||
*/
|
||||
public static String get(String url) throws Exception{
|
||||
//实例化httpclient
|
||||
CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||
CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||
//实例化get方法
|
||||
HttpGet httpget = new HttpGet(url);
|
||||
//请求结果
|
||||
CloseableHttpResponse response = null;
|
||||
CloseableHttpResponse response = null;
|
||||
String content ="";
|
||||
logger.info("流量统计数据请求路径:"+url);
|
||||
//执行get方法
|
||||
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);
|
||||
}
|
||||
try {
|
||||
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);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}finally {
|
||||
if (response != null) {
|
||||
try {
|
||||
response.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
try {
|
||||
httpclient.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
@@ -112,22 +131,43 @@ public class HttpClientUtil {
|
||||
//结果
|
||||
CloseableHttpResponse response = null;
|
||||
String content="";
|
||||
// try {
|
||||
try {
|
||||
//提交的参数
|
||||
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(nvps, "UTF-8");
|
||||
//将参数给post方法
|
||||
httpPost.setEntity(uefEntity);
|
||||
httpPost.setEntity(uefEntity);
|
||||
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();
|
||||
//执行post方法
|
||||
response = httpclient.execute(httpPost);
|
||||
// if(response.getStatusLine().getStatusCode()==200){
|
||||
content = EntityUtils.toString(response.getEntity(),"utf-8");
|
||||
// System.out.println(content);
|
||||
// }
|
||||
// } catch (ClientProtocolException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
} catch (ClientProtocolException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
if (response != null) {
|
||||
try {
|
||||
response.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
try {
|
||||
httpclient.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return content;
|
||||
}
|
||||
/**
|
||||
@@ -167,15 +207,36 @@ public class HttpClientUtil {
|
||||
StringEntity entity = new StringEntity(param, "utf-8");
|
||||
//将参数给post方法
|
||||
httpPatch.setEntity(entity);
|
||||
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();
|
||||
//执行post方法
|
||||
response = httpclient.execute(httpPatch);
|
||||
int status = response.getStatusLine().getStatusCode();
|
||||
if(status==200){
|
||||
content = EntityUtils.toString(response.getEntity());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
if (response != null) {
|
||||
try {
|
||||
response.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
try {
|
||||
httpclient.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
@@ -187,10 +248,11 @@ public class HttpClientUtil {
|
||||
* @return 查询结果数据json
|
||||
*/
|
||||
public static String getMsg(String destUrl, Map<String, Object> params, HttpServletRequest req) {
|
||||
RequestContext requestContext = new RequestContext(req);
|
||||
// RequestContext requestContext = new RequestContext(req);
|
||||
|
||||
// CloseableHttpResponse response = null;
|
||||
String result = null;
|
||||
Response response=null;
|
||||
String url = "";
|
||||
try {
|
||||
URIBuilder uriBuilder = new URIBuilder(destUrl);
|
||||
@@ -208,9 +270,7 @@ public class HttpClientUtil {
|
||||
logger.info("getId url:"+url);
|
||||
//获取响应结果
|
||||
Builder header = wt.request(MediaType.APPLICATION_JSON).header("Content-Type", MediaType.APPLICATION_JSON);
|
||||
Response response= header.get();
|
||||
|
||||
|
||||
response= header.get();
|
||||
|
||||
// HttpGet request = new HttpGet(uriBuilder.build());
|
||||
// request.setConfig(requestConfig);
|
||||
@@ -231,28 +291,12 @@ public class HttpClientUtil {
|
||||
logger.error("获取消息失败,相应内容如下: " + result);
|
||||
throw new MaatConvertException(status+"");
|
||||
}
|
||||
} /*catch (SocketTimeoutException e) {
|
||||
logger.error("获取消息失败 ", e);
|
||||
throw new MaatConvertException(requestContext.getMessage("request_service_failed") + ":timeout");
|
||||
} catch (MaatConvertException e) {
|
||||
throw e;
|
||||
} catch (ConnectTimeoutException e) {
|
||||
logger.error("获取消息失败 ", e);
|
||||
throw new MaatConvertException(requestContext.getMessage("request_service_failed") + ":timeout");
|
||||
}*/ catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
logger.error("获取消息失败,相应内容如下: " + result);
|
||||
logger.error("获取消息失败 ", e);
|
||||
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:");
|
||||
} /*finally {
|
||||
try {
|
||||
if (response != null) {
|
||||
response.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}*/
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -262,7 +306,8 @@ public class HttpClientUtil {
|
||||
* @return
|
||||
*/
|
||||
public static String galaxyMessageFormat(String recv){
|
||||
Map parse = JSON.parseObject(recv,Map.class);
|
||||
Gson gson = new Gson();
|
||||
Map parse = gson.fromJson(recv,Map.class);
|
||||
Map map = (Map) parse.get("data");
|
||||
List reslist=new ArrayList();
|
||||
List<Map<String,Object>> list= (List)map.get("list");
|
||||
@@ -294,7 +339,7 @@ public class HttpClientUtil {
|
||||
}
|
||||
map.put("list", reslist);
|
||||
parse.put("data", map);
|
||||
recv = JSON.toJSONString(parse);
|
||||
recv = gson.toJson(parse);
|
||||
}
|
||||
return recv;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user