package com.nationalelectric.greenH5.utils; import org.apache.commons.httpclient.HttpVersion; import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.commons.io.IOUtils; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; public class HttpClientUtil { /** * HTTP请求超时时间,单位毫秒,默认30秒 */ public static final int TIMEOUT = 30000; /** * 字符编码,默认UTF8 */ public static final String CHARSET = "UTF-8"; public final static String jsonType = "application/json;charset=UTF-8"; private static MultiThreadedHttpConnectionManager cm = new MultiThreadedHttpConnectionManager(); static { // 单主机最大50个连接 cm.getParams().setDefaultMaxConnectionsPerHost(50); // 所有主机合计5000连接 cm.getParams().setMaxTotalConnections(5000); } /** * 获取客户端实例 * * @param * * @param * * @return */ public static HttpClient createHttpClient() throws Exception { HttpClient client = new HttpClient(cm); client.getHttpConnectionManager().getParams() .setConnectionTimeout(TIMEOUT); client.getHttpConnectionManager().getParams().setSoTimeout(TIMEOUT); return client; } /** * 获取POST请求 * * @param url * 资源地址 * @param params * POST提交参数 * @return */ public static PostMethod getPostMethod(String url, Map params) throws Exception { PostMethod postMethod = new PostMethod(url); postMethod.getParams().setSoTimeout(TIMEOUT); postMethod.getParams().setContentCharset(CHARSET); postMethod.getParams().setVersion(HttpVersion.HTTP_1_1); if (null != params && !(params.isEmpty())) { Iterator> iter = params.entrySet().iterator(); System.out.println("Charset.defaultCharset():"+Charset.defaultCharset()); while (iter.hasNext()) { Entry param = iter.next(); //postMethod.addParameter(param.getKey(), URLEncoder.encode(param.getValue(), "UTF-8")); postMethod.addParameter(param.getKey(), param.getValue()); } } return postMethod; } /** * 获取POST请求 * * @param url * 资源地址 * @param body * POST提交参数 * @return */ public static PostMethod getPostMethod(String url, String body) throws Exception { PostMethod postMethod = new PostMethod(url); postMethod.getParams().setSoTimeout(TIMEOUT); postMethod.getParams().setContentCharset(CHARSET); postMethod.getParams().setVersion(HttpVersion.HTTP_1_1); if (null != body && !("".equals(body))) { RequestEntity requestEntity = new StringRequestEntity(body, "application/json", "UTF-8"); postMethod.setRequestEntity(requestEntity); // postMethod.addRequestHeader("Content-Length", // String.valueOf(body.getBytes().length)); // postMethod.setRequestBody(body); } else { postMethod.addRequestHeader("Content-Length", String.valueOf(0)); } return postMethod; } /** * 获取POST请求 * @param url * 资源地址 * @param body * POST提交参数 * @return * @throws Exception */ public static PostMethod getIscPostMethod(String url, String body, String sign, String appId) throws Exception { PostMethod postMethod = new PostMethod(url); postMethod.getParams().setSoTimeout(TIMEOUT); postMethod.getParams().setContentCharset(CHARSET); postMethod.getParams().setVersion(HttpVersion.HTTP_1_1); if (null != body && !("".equals(body))) { postMethod.addRequestHeader("Content-Length", String.valueOf(body.getBytes().length)); postMethod.setRequestBody(body); } else { postMethod.addRequestHeader("Content-Length", String.valueOf(0)); } // 设置头信息 postMethod.addRequestHeader("Content-Type", jsonType); postMethod.addRequestHeader("Accept", "application/json"); postMethod.addRequestHeader("X-Acloud-Data-Sign", sign); postMethod.addRequestHeader("X-Clientid", appId); return postMethod; } /** * 获取POST请求 * @param url * 资源地址 * @param body * POST提交参数 * @param sign * 签名 * @param accessToken * accessToken * @param appId * 系统id * @return * @throws Exception */ public static PostMethod getIscPostMethod2(String url, String body, String sign, String accessToken, String appId) throws Exception { PostMethod postMethod = new PostMethod(url); postMethod.getParams().setSoTimeout(TIMEOUT); postMethod.getParams().setContentCharset(CHARSET); postMethod.getParams().setVersion(HttpVersion.HTTP_1_1); if (null != body && !("".equals(body))) { // postMethod.addRequestHeader("Content-Length", // String.valueOf(body.getBytes().length)); RequestEntity requestEntity = new StringRequestEntity(body, "application/json", "UTF-8"); postMethod.setRequestEntity(requestEntity); // postMethod.setRequestBody(body); } else { postMethod.addRequestHeader("Content-Length", String.valueOf(0)); } // 设置头信息 postMethod.addRequestHeader("Content-Type", jsonType); postMethod.addRequestHeader("Accept", "application/json"); postMethod.addRequestHeader("X-ISC-AccessToken", accessToken); postMethod.addRequestHeader("X-Acloud-Data-Sign", sign); postMethod.addRequestHeader("X-Clientid", appId); return postMethod; } public static String send(String url, Map params) { int statusCode = 0; String re = ""; HttpClient client = null; PostMethod postMethod = null; InputStream in = null; BufferedReader br = null; try { client = createHttpClient(); if (null != client) { postMethod = getPostMethod(url, params); if (null != postMethod) { client.executeMethod(postMethod); statusCode = postMethod.getStatusCode(); System.out.println("=================>>statusCode: " + statusCode); if (HttpStatus.SC_OK == statusCode) { in = postMethod.getResponseBodyAsStream(); br = new BufferedReader(new InputStreamReader(in)); StringBuffer sbf = new StringBuffer(); String result = ""; while ((result = br.readLine())!=null) { sbf.append(result); } re = sbf.toString(); System.out.println("=================>>ResponseBody: " + re); } } } } catch (Exception e) { e.printStackTrace(); } finally { if (null != postMethod) { postMethod.releaseConnection(); } IOUtils.closeQuietly(br); IOUtils.closeQuietly(in); } System.out.println("exit method com.syit.util.HttpClientUtil.send"); return re; } public static String send2(String url, String body) { int statusCode = 0; String re = ""; HttpClient client = null; PostMethod postMethod = null; InputStream in = null; BufferedReader br = null; try { client = createHttpClient(); if (null != client) { postMethod = getPostMethod(url, body); if (null != postMethod) { client.executeMethod(postMethod); statusCode = postMethod.getStatusCode(); System.out.println("=================>>statusCode: " + statusCode); if (HttpStatus.SC_OK == statusCode) { in = postMethod.getResponseBodyAsStream(); br = new BufferedReader(new InputStreamReader(in)); StringBuffer sbf = new StringBuffer(); String result = ""; while ((result = br.readLine())!=null) { sbf.append(result); } re = sbf.toString(); System.out.println("ResponseBody: " + re); } } } } catch (Exception e) { e.printStackTrace(); } finally { if (null != postMethod) { postMethod.releaseConnection(); } IOUtils.closeQuietly(br); IOUtils.closeQuietly(in); } return re; } public static String iscSend(String url, String body, String sign, String appId) { int statusCode = 0; String re = ""; HttpClient client = null; PostMethod postMethod = null; InputStream in = null; BufferedReader br = null; try { client = createHttpClient(); if (null != client) { postMethod = getIscPostMethod(url, body, sign, appId); if (null != postMethod) { client.executeMethod(postMethod); statusCode = postMethod.getStatusCode(); System.out.println("=================>>statusCode: " + statusCode); if (HttpStatus.SC_OK == statusCode) { in = postMethod.getResponseBodyAsStream(); br = new BufferedReader(new InputStreamReader(in)); StringBuffer sbf = new StringBuffer(); String result = ""; while ((result = br.readLine())!=null) { sbf.append(result); } re = sbf.toString(); System.out.println("ResponseBody: " + re); } } } } catch (Exception e) { e.printStackTrace(); } finally { if (null != postMethod) { postMethod.releaseConnection(); } IOUtils.closeQuietly(br); IOUtils.closeQuietly(in); } return re; } public static String iscSend2(String url, String body, String sign, String accessToken, String appId) { int statusCode = 0; String re = ""; HttpClient client = null; PostMethod postMethod = null; InputStream in = null; BufferedReader br = null; try { client = createHttpClient(); if (null != client) { postMethod = getIscPostMethod2(url, body, sign, accessToken, appId); if (null != postMethod) { client.executeMethod(postMethod); statusCode = postMethod.getStatusCode(); System.out.println("=================>>statusCode: " + statusCode); if (HttpStatus.SC_OK == statusCode) { in = postMethod.getResponseBodyAsStream(); br = new BufferedReader(new InputStreamReader(in)); StringBuffer sbf = new StringBuffer(); String result = ""; while ((result = br.readLine())!=null) { sbf.append(result); } re = sbf.toString(); //System.out.println("ResponseBody: " + re); } } } } catch (Exception e) { e.printStackTrace(); } finally { if (null != postMethod) { postMethod.releaseConnection(); } IOUtils.closeQuietly(br); IOUtils.closeQuietly(in); } return re; } }