HttpClientHelper_PushClient
2016-08-04 10:05
176 查看
this is httpClinetHelper
this is PushClient
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; }
相关文章推荐
- HttpClientHelper_PushClient
- C#微信开发之旅(二):基础类之HttpClientHelper(更新:SSL安全策略)
- C# HttpClientHelper请求
- HttpClientHelper
- CHttpClient - A Helper Class Using WinInet
- httpclient https忽略证书
- Android Asynchronous Http Client - 开源http 多线程引擎
- httpclient 超时时间 等待时间 响应时间
- OkHttpClient
- Spring中@Scheduled和HttpClient的连环坑
- Android HttpClient框架get和post方式提交数据(非原创)
- HttpClient请求DefaultHttpClient过时替换
- 一个android访问http资源的便捷工具类——HttpHelper
- HttpClient的认证(转载)
- httpclient 302 重定向
- Android Asynchronous Http Client--Android 开源的网络异步加载类
- android-HttpClient上传信息(包括图片)到服务端
- android HttpClient,DefaultHttpClient,AbstractHttpClient之间关系
- 解决android api23无Apache HttpClient包问题