HttpClientHelper_PushClient
来源:互联网 发布:拓扑算法 编辑:程序博客网 时间:2024/06/03 21:36
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;}
0 0
- HttpClientHelper_PushClient
- Scala协变逆变上界下界
- php mysql数据库备份1
- 使用UDP建立群聊系统
- 剑指Offer:包含min函数的栈
- asp.net NPOI 导出
- HttpClientHelper_PushClient
- Fragment取消预加载
- ftp ssh http telnet https服务及端口
- 南阳理工ACM596
- "svn: E200033: database is locked"解决办法。(转)
- Scala Actor并发编程
- linux桌面操作系统安装eagle
- 【CDOJ 1355】柱爷与三叉戟不得不说的故事 【状压DP+子集枚举】
- 【数据结构与算法学习笔记】PART2 向量(接口与实现,可扩充向量,无序向量,有序向量)