package com.qxueyou.scc.base.util; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URI; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.CookieSpecs; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.RequestBuilder; import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.PlainConnectionSocketFactory; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.entity.BasicHttpEntity; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; /** * @author xiadehu */ public class HttpClient { private static final String UTF_8 = "UTF-8"; private CloseableHttpClient httpClient; private RequestConfig requestConfig; protected static final Logger log = LogManager.getLogger("HttpClient"); private static final int timeout = 30000; public HttpClient() { init(); } public CloseableHttpClient getHttpClient() { return httpClient; } public void setHttpClient(CloseableHttpClient httpClient) { this.httpClient = httpClient; } private void init() { X509TrustManager xtm = new X509TrustManager() { // 创建TrustManager public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; } }; try { // TLS1.0与SSL3.0基本上没有太大的差别,可粗略理解为TLS是SSL的继承者,但它们使用的是相同的SSLContext SSLContext ctx = SSLContext.getInstance("TLS"); // 使用TrustManager来初始化该上下文,TrustManager只是被SSL的Socket所使用 ctx.init(null, new TrustManager[] { xtm }, null); /* * SchemeRegistry schemeRegistry = new SchemeRegistry(); * schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory * .getSocketFactory())); schemeRegistry.register(new * Scheme("https", 443, new SSLSocketFactory(ctx))); */ /* * PoolingClientConnectionManager cm = new * PoolingClientConnectionManager(schemeRegistry); * cm.setMaxTotal(200); cm.setDefaultMaxPerRoute(20); */ // httpClient = new DefaultHttpClient(cm); /* * httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, * CookiePolicy.BEST_MATCH); httpClient.getParams().setParameter( * ClientPNames.CONN_MANAGER_TIMEOUT, Long.valueOf(timeout)); * * HttpParams params = httpClient.getParams(); * HttpConnectionParams.setConnectionTimeout(params, timeout); * HttpConnectionParams.setSoTimeout(params, timeout); */ // cyq modify begin PlainConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory(); SSLConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory(); Registry r = RegistryBuilder. create() .register("http", plainsf).register("https", sslsf).build(); PoolingHttpClientConnectionManager ccm = new PoolingHttpClientConnectionManager(r); ccm.setMaxTotal(200); ccm.setDefaultMaxPerRoute(20); httpClient = HttpClients.custom().setConnectionManager(ccm).build(); requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).setConnectTimeout(timeout) .setSocketTimeout(timeout).build(); // end } catch (KeyManagementException e) { log.error(e, e); } catch (NoSuchAlgorithmException e) { log.error(e, e); } catch (ParseException e) { log.error(e, e); } } /** * 向HTTPS地址发送POST请求 * * @param reqURL * 请求地址 * @param params * 请求参数 * @return 响应内容 */ @SuppressWarnings("unchecked") public String sendStringPostRequest(String reqURL, Map params1) { Map params = params1; try { if (params == null) { params = Collections.EMPTY_MAP; } HttpEntity entity = sendPostRequest(reqURL, params); // 获取响应实体 if( null != entity ){ String content = EntityUtils.toString(entity, UTF_8); EntityUtils.consume(entity); return content; } } catch (UnsupportedEncodingException e) { log.error(e, e); } catch (ClientProtocolException e) { log.error(e, e); } catch (ParseException e) { log.error(e, e); } catch (IOException e) { log.error(e, e); } return null; } /** * 向HTTPS地址发送POST请求 * * @param reqURL * 请求地址 * @param params * 请求参数 * @return 响应内容 */ @SuppressWarnings("unchecked") public String sendStringPostRequest(String reqURL, String data) { try { StringEntity reqEntity = new StringEntity(data, UTF_8); HttpEntity entity = sendPostRequest(reqURL, Collections.EMPTY_MAP, reqEntity); // 获取响应实体 if( null != entity ){ String content = EntityUtils.toString(entity, UTF_8); EntityUtils.consume(entity); return content; } } catch (UnsupportedEncodingException e) { log.error(e, e); } catch (ClientProtocolException e) { log.error(e, e); } catch (ParseException e) { log.error(e, e); } catch (IOException e) { log.error(e, e); } return null; } /** * 向HTTPS地址发送POST请求 * * @param reqURL * 请求地址 * @param params * 请求参数 * @return 响应内容 */ public String sendStringPostRequest(String reqURL, Map headerParas1, String data) { Map headerParas = headerParas1; try { StringEntity reqEntity = new StringEntity(data, UTF_8); HttpEntity entity = sendPostRequest(reqURL, headerParas, reqEntity); // 获取响应实体 if( null != entity ){ String content = EntityUtils.toString(entity, UTF_8); EntityUtils.consume(entity); return content; } } catch (UnsupportedEncodingException e) { log.error(e, e); } catch (ClientProtocolException e) { log.error(e, e); } catch (ParseException e) { log.error(e, e); } catch (IOException e) { log.error(e, e); } return null; } /** * 向HTTPS地址发送POST请求 * * @param reqURL * 请求地址 * @param params * 请求参数 * @return 响应内容 */ @SuppressWarnings("unchecked") public HttpEntity sendPostRequest(String reqURL, Map params1) { Map params = params1; if (params == null) { params = Collections.EMPTY_MAP; } List lstParams = new ArrayList(params.size()); for (String key : params.keySet()) { lstParams.add(new BasicNameValuePair(key, params.get(key))); } return sendPostRequest(reqURL, Collections.EMPTY_MAP, lstParams); } @SuppressWarnings("unchecked") public HttpEntity sendPostRequest(String reqURL, Map headerParas1, Map params1) { Map params = params1; Map headerParas = headerParas1; if (params == null) { params = Collections.EMPTY_MAP; } headerParas = checkUserAgent(headerParas); List lstParams = new ArrayList(params.size()); for (String key : params.keySet()) { lstParams.add(new BasicNameValuePair(key, params.get(key))); } return sendPostRequest(reqURL, headerParas, lstParams); } public HttpEntity sendPostRequest(String reqURL, Map headerParas, List params) { try { HttpEntity entity = new UrlEncodedFormEntity(params, UTF_8); return sendPostRequest(reqURL, headerParas, entity); } catch (UnsupportedEncodingException e) { log.error(e, e); } return null; } public HttpEntity sendPostRequest(String reqURL, Map headerParas1, HttpEntity reqEntity) { Map headerParas = headerParas1; try { HttpPost httpPost = new HttpPost(reqURL); // 创建HttpPost httpPost.setEntity(reqEntity); headerParas = checkUserAgent(headerParas); for (String key : headerParas.keySet()) { httpPost.setHeader(key, headerParas.get(key)); } Header[] headers = httpPost.getAllHeaders(); for (Header header : headers) { log.debug("request--header:" + header.getName() + ":" + header.getValue()); } HttpResponse response = httpClient.execute(httpPost); // 执行POST请求 headers = response.getAllHeaders(); for (Header header : headers) { log.debug("response--header:" + header.getName() + ":" + header.getValue()); } HttpEntity entity = response.getEntity(); // 获取响应实体 this.debug(httpPost.getURI(), response.getStatusLine().getStatusCode(), entity.getContentLength(), entity.getContentType()); if (302 == response.getStatusLine().getStatusCode() && response.containsHeader("location")) { String location = response.getLastHeader("location").getValue(); log.debug("重定向:" + location); EntityUtils.consume(entity); return sendPostRequest(location, headerParas, reqEntity); } return entity; } catch (UnsupportedEncodingException e) { log.error(e, e); } catch (ClientProtocolException e) { log.error(e, e); } catch (ParseException e) { log.error(e, e); } catch (IOException e) { log.error(e, e); } return null; } public String sendStringDeleteRequest(String reqURL, Map headerParas1) { Map headerParas = headerParas1; try { HttpDelete httpDelete = new HttpDelete(reqURL); // 创建HttpPost headerParas = checkUserAgent(headerParas); for (String key : headerParas.keySet()) { httpDelete.setHeader(key, headerParas.get(key)); } Header[] headers = httpDelete.getAllHeaders(); for (Header header : headers) { log.debug("request--header:" + header.getName() + ":" + header.getValue()); } HttpResponse response = httpClient.execute(httpDelete); // 执行POST请求 headers = response.getAllHeaders(); for (Header header : headers) { log.debug("response--header:" + header.getName() + ":" + header.getValue()); } HttpEntity entity = response.getEntity(); // 获取响应实体 this.debug(httpDelete.getURI(), response.getStatusLine().getStatusCode(), entity.getContentLength(), entity.getContentType()); if (302 == response.getStatusLine().getStatusCode() && response.containsHeader("location")) { return null; } return EntityUtils.toString(entity); } catch (UnsupportedEncodingException e) { log.error(e, e); } catch (ClientProtocolException e) { log.error(e, e); } catch (ParseException e) { log.error(e, e); } catch (IOException e) { log.error(e, e); } return null; } /** * debug log * * @param uri * @param code * @param length * @param header */ private void debug(URI uri, int code, long length, Header header){ log.debug("请求地址: " + uri); log.debug("响应状态: " + code); log.debug("响应长度: " + length); log.debug("响应类型: " + header); } public String sendStringPutRequest(String reqURL, Map headerParas1, String body) { Map headerParas = headerParas1; try { HttpPut httpPut = new HttpPut(reqURL); // 创建HttpPost headerParas = checkUserAgent(headerParas); for (String key : headerParas.keySet()) { httpPut.setHeader(key, headerParas.get(key)); } Header[] headers = httpPut.getAllHeaders(); for (Header header : headers) { log.debug("request--header:" + header.getName() + ":" + header.getValue()); } StringEntity reqEntity = new StringEntity(body, UTF_8); httpPut.setEntity(reqEntity); HttpResponse response = httpClient.execute(httpPut); // 执行POST请求 headers = response.getAllHeaders(); for (Header header : headers) { log.debug("response--header:" + header.getName() + ":" + header.getValue()); } HttpEntity entity = response.getEntity(); // 获取响应实体 this.debug(httpPut.getURI(), response.getStatusLine().getStatusCode(), entity.getContentLength(), entity.getContentType()); if (302 == response.getStatusLine().getStatusCode() && response.containsHeader("location")) { return null; } return EntityUtils.toString(entity); } catch (UnsupportedEncodingException e) { log.error(e, e); } catch (ClientProtocolException e) { log.error(e, e); } catch (ParseException e) { log.error(e, e); } catch (IOException e) { log.error(e, e); } return null; } public HttpEntity sendPostRequest(String reqURL, Map headerParas1, String param) { Map headerParas = headerParas1; try { HttpPost httpPost = new HttpPost(reqURL); // 创建HttpPost BasicHttpEntity requestBody = new BasicHttpEntity(); requestBody.setContent(new ByteArrayInputStream(param.getBytes(UTF_8))); requestBody.setContentLength(param.getBytes(UTF_8).length); httpPost.setEntity(requestBody); headerParas = checkUserAgent(headerParas); for (String key : headerParas.keySet()) { httpPost.setHeader(key, headerParas.get(key)); } Header[] headers = httpPost.getAllHeaders(); for (Header header : headers) { log.debug("request--header:" + header.getName() + ":" + header.getValue()); } log.debug("request--param:" + param); HttpResponse response = httpClient.execute(httpPost); // 执行POST请求 headers = response.getAllHeaders(); for (Header header : headers) { log.debug("response--header:" + header.getName() + ":" + header.getValue()); } HttpEntity entity = response.getEntity(); // 获取响应实体 this.debug(httpPost.getURI(), response.getStatusLine().getStatusCode(), entity.getContentLength(), entity.getContentType()); if (302 == response.getStatusLine().getStatusCode() && response.containsHeader("location")) { String location = response.getLastHeader("location").getValue(); log.debug("重定向:" + location); EntityUtils.consume(entity); return sendPostRequest(location, headerParas, param); } return entity; } catch (UnsupportedEncodingException e) { log.error(e, e); } catch (ClientProtocolException e) { log.error(e, e); } catch (ParseException e) { log.error(e, e); } catch (IOException e) { log.error(e, e); } return null; } public String sendStringGetRequest(String reqURL, Map headerParas, Map params) { try { HttpEntity entity = sendGetRequest(reqURL, headerParas, params); if( null != entity ){ return EntityUtils.toString(entity, UTF_8); } } catch (ParseException e) { log.error(e, e); } catch (IOException e) { log.error(e, e); } return null; } public HttpEntity sendGetRequest(String reqURL, Map headerParas, Map params) { HttpResponse response = this.sendGetRequestAsResponse(reqURL, headerParas, params); if(response != null){ return response.getEntity(); } return null; } private Map checkUserAgent(Map headerParas1) { Map headerParas = headerParas1; if (headerParas == null || headerParas.isEmpty()) { headerParas = new HashMap(1); } String agentKey = "User-Agent"; if (headerParas.containsKey(agentKey)) { headerParas.remove(agentKey); } headerParas.put(agentKey, "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko"); return headerParas; } @SuppressWarnings("unchecked") public HttpResponse sendGetRequestAsResponse(String reqURL, Map headerParas1, Map params1) { Map params = params1; Map headerParas = headerParas1; try { // HttpGet httpGet = new HttpGet(reqURL); // 创建HttpPost headerParas = checkUserAgent(headerParas); if (params == null) { params = Collections.EMPTY_MAP; } // begin RequestBuilder requestBuilder = RequestBuilder.get().setConfig(requestConfig).setUri(reqURL); for (String key : headerParas.keySet()) { requestBuilder.addHeader(key, headerParas.get(key)); } for (String key : params.keySet()) { requestBuilder.addParameter(key, params.get(key)); } HttpUriRequest request = requestBuilder.build(); // end // Header[] headers = httpGet.getAllHeaders(); Header[] headers = request.getAllHeaders(); for (Header header : headers) { log.debug("request--header:" + header.getName() + ":" + header.getValue()); } // HttpResponse response = httpClient.execute(httpGet); // 执行POST请求 HttpResponse response = httpClient.execute(request); headers = response.getAllHeaders(); for (Header header : headers) { log.debug("response--header:" + header.getName() + ":" + header.getValue()); } HttpEntity entity = response.getEntity(); // 获取响应实体 this.debug(request.getURI(), response.getStatusLine().getStatusCode(), entity.getContentLength(), entity.getContentType()); if (302 == response.getStatusLine().getStatusCode() && response.containsHeader("location")) { String location = response.getLastHeader("location").getValue(); log.debug("重定向"); return sendGetRequestAsResponse(location, headerParas, params); } return response; } catch (UnsupportedEncodingException e) { log.error(e, e); } catch (ClientProtocolException e) { log.error(e, e); } catch (ParseException e) { log.error(e, e); } catch (IOException e) { log.error(e, e); } return null; } }