| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- package com.happy.Unitil_elc;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpHost;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.config.RequestConfig;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.config.Registry;
- import org.apache.http.config.RegistryBuilder;
- import org.apache.http.config.SocketConfig;
- import org.apache.http.conn.socket.ConnectionSocketFactory;
- import org.apache.http.conn.socket.PlainConnectionSocketFactory;
- import org.apache.http.conn.ssl.NoopHostnameVerifier;
- import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
- import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClientBuilder;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
- import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
- import org.apache.http.message.BasicHeader;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.protocol.HTTP;
- import org.apache.http.ssl.SSLContexts;
- import org.apache.http.util.EntityUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import javax.net.ssl.SSLContext;
- import java.io.IOException;
- import java.nio.charset.Charset;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class HttpClientHelper {
- private static final Logger logger = LoggerFactory.getLogger(HttpClientHelper.class);
- private static PoolingHttpClientConnectionManager poolConnManager = null;
- private int maxTotalPool = 200;
- private int maxConPerRoute = 20;
- private int socketTimeout = 100000;
- private int connectionRequestTimeout = 90000;
- private int connectTimeout = 90000;
- // 代理信息
- public static String proxy_ip;
- public static int proxy_port;
- private static HttpClientHelper httpClientHelper = null;
- private HttpClientHelper() {
- init();
- }
- private static synchronized void syncInit() {
- if (httpClientHelper == null) {
- httpClientHelper = new HttpClientHelper();
- }
- }
- public static HttpClientHelper getInstance(String proxyIp, Integer proxyPort) {
- proxy_port = proxyPort;
- proxy_ip = proxyIp;
- if (httpClientHelper == null) {
- syncInit();
- }
- return httpClientHelper;
- }
- public static HttpClientHelper getInstance() {
- if (httpClientHelper == null) {
- syncInit();
- }
- return httpClientHelper;
- }
- public PoolingHttpClientConnectionManager getConnManager() {
- PoolingHttpClientConnectionManager cm = null;
- try {
- SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
- SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, NoopHostnameVerifier.INSTANCE);
- Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http",
- PlainConnectionSocketFactory.getSocketFactory()).register("https",
- sslsf).build();
- cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
- // 将最大连接数增加到200
- cm.setMaxTotal(maxTotalPool);
- // 将每个路由基础的连接增加到20
- cm.setDefaultMaxPerRoute(maxConPerRoute);
- } catch (Exception e) {
- logger.error("InterfacePhpUtilManager init Exception" + e.toString());
- }
- return cm;
- }
- public void init() {
- try {
- SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
- SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, NoopHostnameVerifier.INSTANCE);
- Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http",
- PlainConnectionSocketFactory.getSocketFactory()).register("https",
- sslsf).build();
- poolConnManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
- // Increase max total connection to 200
- poolConnManager.setMaxTotal(maxTotalPool);
- // Increase default max connection per route to 20
- poolConnManager.setDefaultMaxPerRoute(maxConPerRoute);
- SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(socketTimeout).build();
- poolConnManager.setDefaultSocketConfig(socketConfig);
- } catch (Exception e) {
- logger.error("InterfacePhpUtilManager init Exception" + e.toString());
- }
- }
- public CloseableHttpClient getConnection() {
- RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(connectionRequestTimeout).setConnectTimeout(connectTimeout).setSocketTimeout(socketTimeout).build();
- HttpClientBuilder httpClientBuilder = HttpClients.custom();
- httpClientBuilder.setConnectionManager(poolConnManager).setDefaultRequestConfig(requestConfig);// set proxy
- if (!StringUtil.isEmpty(proxy_ip) && proxy_port > 0) {
- HttpHost proxy = new HttpHost(proxy_ip, proxy_port);
- DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
- httpClientBuilder.setRoutePlanner(routePlanner);
- }
- CloseableHttpClient httpClient = httpClientBuilder.build();
- if (poolConnManager != null && poolConnManager.getTotalStats() != null) {
- logger.info("now client pool " + poolConnManager.getTotalStats().toString());
- }
- return httpClient;
- }
- /**
- * 发送 GET 请求(HTTP),不带输入数据
- */
- public String doGet(String url) {
- return doGet(url, new HashMap<String,String>());
- }
- /**
- * 发送 GET 请求(HTTP),K-V形式
- */
- public String doGet(String url, Map<String, String> params) {
- String apiUrl = url;
- StringBuffer param = new StringBuffer();
- int i = 0;
- for (String key : params.keySet()) {
- if (i == 0) {
- param.append("?");
- } else {
- param.append("&");
- }
- param.append(key).append("=").append(params.get(key));
- i++;
- }
- apiUrl += param;
- logger.info(apiUrl);
- String result = null;
- CloseableHttpClient httpClient = getConnection();
- CloseableHttpResponse response = null;
- HttpGet httpPost = null;
- try {
- httpPost = new HttpGet(apiUrl);
- response = httpClient.execute(httpPost);
- int status = response.getStatusLine().getStatusCode();
- logger.info("http request url : " + url + " status : " + status);
- if (status >= 200 && status < 300) {
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- result = EntityUtils.toString(response.getEntity(), "UTF-8");
- logger.info("Request result : " + result);
- }
- }
- EntityUtils.consume(response.getEntity());
- response.close();
- return result;
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- } finally {
- httpPost.releaseConnection();
- if (response != null) {
- try {
- EntityUtils.consume(response.getEntity());
- response.close();
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- }
- }
- }
- return result;
- }
- public String doPost(String url) {
- return doPost(url, new HashMap<String, String>());
- }
- /**
- * 发送 POST json
- *
- * @param url 接口URL
- */
- public String doPostJson(String url, String jsonstr) {
- String result = null;
- HttpPost httpPost = new HttpPost(url);
- CloseableHttpClient httpClient = getConnection();
- CloseableHttpResponse response = null;
- try {
- StringEntity se = new StringEntity(jsonstr, Charset.forName("UTF-8"));
- se.setContentType("text/json");
- se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
- httpPost.setEntity(se);
- response = httpClient.execute(httpPost);
- int status = response.getStatusLine().getStatusCode();
- logger.info("http request url : " + url + " status : " + status);
- if (status >= 200 && status < 300) {
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- result = EntityUtils.toString(response.getEntity(), "UTF-8");
- logger.info("Request result : " + result);
- }
- }
- EntityUtils.consume(response.getEntity());
- response.close();
- return result;
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- } finally {
- httpPost.releaseConnection();
- if (response != null) {
- try {
- EntityUtils.consume(response.getEntity());
- response.close();
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- }
- }
- }
- return result;
- }
- /**
- * 发送 POST 请求(HTTP),K-V形式
- *
- * @param url 接口URL
- * @param params 参数map
- * @return
- */
- public String doPost(String url, Map<String, String> params) {
- String result = null;
- HttpPost httpPost = new HttpPost(url);
- CloseableHttpClient httpClient = getConnection();
- CloseableHttpResponse response = null;
- try {
- List<NameValuePair> pairList = new ArrayList<>(params.size());
- for (Map.Entry<String, String> entry : params.entrySet()) {
- NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue());
- pairList.add(pair);
- }
- httpPost.setEntity(new UrlEncodedFormEntity(pairList, Charset.forName("UTF-8")));
- response = httpClient.execute(httpPost);
- int status = response.getStatusLine().getStatusCode();
- logger.info("http request url : " + url + " status : " + status);
- if (status >= 200 && status < 300) {
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- result = EntityUtils.toString(response.getEntity(), "UTF-8");
- logger.info("Request result : " + result);
- }
- }
- EntityUtils.consume(response.getEntity());
- response.close();
- return result;
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- } finally {
- httpPost.releaseConnection();
- if (response != null) {
- try {
- EntityUtils.consume(response.getEntity());
- response.close();
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- }
- }
- }
- return result;
- }
- /**
- * 发送 POST 请求(HTTP),K-V形式
- *
- * @param url 接口URL
- * @param params 参数map
- * @return
- */
- public String doPost(String url, Map<String, String> params, String token) {
- String result = null;
- HttpPost httpPost = new HttpPost(url);
- CloseableHttpClient httpClient = getConnection();
- CloseableHttpResponse response = null;
- try {
- List<NameValuePair> pairList = new ArrayList<>(params.size());
- for (Map.Entry<String, String> entry : params.entrySet()) {
- NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue());
- pairList.add(pair);
- }
- httpPost.setEntity(new UrlEncodedFormEntity(pairList, Charset.forName("UTF-8")));
- httpPost.addHeader("token", token);
- response = httpClient.execute(httpPost);
- int status = response.getStatusLine().getStatusCode();
- logger.info("http request url : " + url + " status : " + status);
- if (status >= 200 && status < 300) {
- HttpEntity entity = response.getEntity();
- if (entity != null) {
- result = EntityUtils.toString(response.getEntity(), "UTF-8");
- logger.info("Request result : " + result);
- }
- }
- EntityUtils.consume(response.getEntity());
- response.close();
- return result;
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- } finally {
- httpPost.releaseConnection();
- if (response != null) {
- try {
- EntityUtils.consume(response.getEntity());
- response.close();
- } catch (IOException e) {
- logger.error(e.getMessage(), e);
- }
- }
- }
- return result;
- }
- }
|