this is httpClinetHelper
private static HttpClient httpClient;
public static HttpClient gethttpClient() {
if (httpClient == null)
initHttpClient();
return httpClient;
}
/**
* @Title: initHttpClient
* @Description: httpclient
* @throws
*/
private static synchronized void initHttpClient() {
// 创建httpclient连接池
PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager();
httpClientConnectionManager.setMaxTotal(100); // 设置连接池线程最大数量
httpClientConnectionManager.setDefaultMaxPerRoute(20); // 设置单个路由最大的连接线程数量
// 创建http request的配置信息
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(30 * 1000)
.setSocketTimeout(30 * 1000).build();
// 设置重定向策略
LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy();
// 初始化httpclient客户端
httpClient = HttpClients.custom()
.setConnectionManager(httpClientConnectionManager)
.setDefaultRequestConfig(requestConfig)
.setRedirectStrategy(redirectStrategy).build();
LOG.info("initHttpClient success......");
}
this is PushClient
HttpClient httpClient = HttpClientHelper.gethttpClient();
/**
* @Title: getJSON
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param url
* @return
* @throws ClientProtocolException
* @throws IOException
* @throws
*/
public String getJSON(String url) throws ClientProtocolException, IOException {
HttpGet get = new HttpGet(url);
HttpResponse response = null;
LOG.info("HttpClient->get:"+url);
String result = null;
response = httpClient.execute(get);
result = IOUtils.toString(new InputStreamReader(response
.getEntity().getContent(), "utf-8"));
LOG.info("HttpClient->get:"+result);
return result;
}
/**
* @Title: postJSON
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param object
* @param url
* @return
* @throws ClientProtocolException
* @throws IOException
* @throws
*/
public String postJSON(Object object, String url) throws ClientProtocolException, IOException {
String result = null;
HttpResponse response = null;
try{
HttpPost httpPost = new HttpPost(url);
String json = JSON.toJSONString(object);
LOG.info("HttpClient->post:"+url);
if(null != json){
LOG.info("HttpClient->post:"+json);
StringEntity entity = new StringEntity(json,"utf-8");//解决中文乱码问题
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
}
response = httpClient.execute(httpPost);
result = IOUtils.toString(new InputStreamReader(response
.getEntity().getContent(), "utf-8"));
LOG.info("HttpClient->post:"+result);
return result;
}catch (Exception e){
throw e;
}finally{
if(response!=null){
EntityUtils.consumeQuietly(response.getEntity());
}
//关闭空闲超过30秒的连接
httpClient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);
}
}
/**
* @Title: postData
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param map
* @param url
* @return
* @throws ClientProtocolException
* @throws IOException
* @throws
*/
@SuppressWarnings("deprecation")
public String postData(Map<String,String> map, String url) throws ClientProtocolException, IOException {
HttpPost httpPost = new HttpPost(url);
HttpResponse response = null;
String result = null;
if(null != map && map.size()>0){
List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
Set<String> keySet = map.keySet();
for(String key : keySet) {
nvps.add(new BasicNameValuePair(key, map.get(key)));
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
}
response = httpClient.execute(httpPost);
result = IOUtils.toString(new InputStreamReader(response
.getEntity().getContent(), "utf-8"));
LOG.info("HttpClient->post:"+result);
return result;
}
public String post(Object o, String url) throws ClientProtocolException, IOException {
org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
PostMethod method = new PostMethod(url);
int statusCode;
String responseCharSet ="";
String responseString = "";
try {
String json = o.toString();
StringRequestEntity entity = new StringRequestEntity(json,"application/json","utf-8");//解决中文乱码问题
method.setRequestEntity(entity);
//method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
statusCode = httpClient.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
LOG.error(">>>>>>>>>>>>>> Method failed: " + method.getStatusLine());
LOG.error(">>>>>>>>>>>>>> Http服务链路异常:服务器状态码为" + statusCode);
}
responseCharSet = method.getResponseCharSet();
responseString = method.getResponseBodyAsString();
if (responseCharSet.equals("ISO-8859-1")) {
responseString = new String(responseString.getBytes(responseCharSet), "UTF-8");
}
} catch (Throwable e) {
LOG.error(">>>>>>>>>>>>>> Http服务链路异常:" + e.getMessage() + e);
} finally {
method.releaseConnection();
}
return responseString;
}