准备祖传了,有问题欢迎大家指正。
HttpUtil
import com.txlc.cloud.commons.exception.ServiceException;
import com.txlc.dwh.common.constants.MyErrorCode;
import org.ssssssss.script.annotation.Comment;import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
public class HttpUtil {public static final String UTF_8 = "UTF-8";private static final PooledHttpClientAdaptor adaptor = new PooledHttpClientAdaptor();@Comment("http get")public static String doGet(@Comment("url")String apiUrl, @Comment("请求头,没有请传null")Map<String, String> headers, @Comment("url参数")Map<String, Object> params) {return adaptor.doGet(apiUrl, headers, params);}public static String doFormPost(@Comment("url")String apiUrl, @Comment("请求头,没有请传null")Map<String, String> headers, @Comment("form参数")Map<String, Object> params) {return adaptor.doPost(apiUrl, headers, params);}public static String doJsonPost(@Comment("url")String apiUrl,@Comment("请求头,没有请传null") Map<String, String> headers, @Comment("json参数")String jsonParam) {try {return adaptor.doPost(apiUrl, headers, jsonParam);} catch (UnsupportedEncodingException e) {throw new ServiceException(MyErrorCode.HTTP_PARAM_JSON.getStatus(),MyErrorCode.HTTP_PARAM_JSON.getMsg());}}public static String doDelete(String url, Map<String, String> headers, HashMap<String, Object> params) {return adaptor.doDelete(url, headers, params);}public static String getUrlWithParams(String url, Map<String, Object> params) {boolean first = true;StringBuilder sb = new StringBuilder(url);for (String key : params.keySet()) {char ch = '&';if (first == true) {ch = '?';first = false;}String value = params.get(key).toString();try {String sval = URLEncoder.encode(value, UTF_8);sb.append(ch).append(key).append("=").append(sval);} catch (UnsupportedEncodingException e) {}}return sb.toString();}public static Map<String, Object> convent2Map(Object b) {Map<String, Object> params = new HashMap<>();for(Field field: b.getClass().getDeclaredFields()) {field.setAccessible(true);Object val = null;try {val = field.get(b);} catch (IllegalArgumentException | IllegalAccessException e) {}if(val != null) {params.put(field.getName(), val);}}return params;}}
PooledHttpClientAdaptor
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;import javax.net.ssl.SSLContext;import cn.hutool.core.util.StrUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
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.HttpDelete;
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.conn.HttpClientConnectionManager;
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.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.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import com.alibaba.fastjson.JSON;
public class PooledHttpClientAdaptor {private static final Logger logger = LoggerFactory.getLogger(PooledHttpClientAdaptor.class);private static final int DEFAULT_POOL_MAX_TOTAL = 200;private static final int DEFAULT_POOL_MAX_PER_ROUTE = 200;private static final int DEFAULT_CONNECT_TIMEOUT = 10000;private static final int DEFAULT_CONNECT_REQUEST_TIMEOUT = 10000;private static final int DEFAULT_SOCKET_TIMEOUT = 60000;private PoolingHttpClientConnectionManager gcm = null;private CloseableHttpClient httpClient = null;private IdleConnectionMonitorThread idleThread = null;private final int maxTotal;private final int maxPerRoute;private final int connectTimeout;private final int connectRequestTimeout;private final int socketTimeout;public PooledHttpClientAdaptor() {this(PooledHttpClientAdaptor.DEFAULT_POOL_MAX_TOTAL,PooledHttpClientAdaptor.DEFAULT_POOL_MAX_PER_ROUTE,PooledHttpClientAdaptor.DEFAULT_CONNECT_TIMEOUT,PooledHttpClientAdaptor.DEFAULT_CONNECT_REQUEST_TIMEOUT,PooledHttpClientAdaptor.DEFAULT_SOCKET_TIMEOUT);}public PooledHttpClientAdaptor(int maxTotal, int maxPerRoute, int connectTimeout, int connectRequestTimeout, int socketTimeout ) {this.maxTotal = maxTotal;this.maxPerRoute = maxPerRoute;this.connectTimeout = connectTimeout;this.connectRequestTimeout = connectRequestTimeout;this.socketTimeout = socketTimeout;final SSLConnectionSocketFactory sslsf;try {sslsf = new SSLConnectionSocketFactory(SSLContext.getDefault(),NoopHostnameVerifier.INSTANCE);} catch (NoSuchAlgorithmException e) {throw new RuntimeException(e);}Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslsf).build();this.gcm = new PoolingHttpClientConnectionManager(registry);this.gcm.setMaxTotal(this.maxTotal);this.gcm.setDefaultMaxPerRoute(this.maxPerRoute);RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(this.connectTimeout) .setSocketTimeout(this.socketTimeout) .setConnectionRequestTimeout(this.connectRequestTimeout) .build();HttpClientBuilder httpClientBuilder = HttpClients.custom();httpClient = httpClientBuilder.setConnectionManager(this.gcm).setDefaultRequestConfig(requestConfig).build();idleThread = new IdleConnectionMonitorThread(this.gcm);idleThread.start();}public String doGet(String url) {return this.doGet(url, Collections.emptyMap(), Collections.emptyMap());}public String doGet(String url, Map<String, Object> params) {return this.doGet(url, Collections.emptyMap(), params);}public String doGet(String url, Map<String, String> headers,Map<String, Object> params) {logger.debug("doGet url:" + url + ". headers :" + JSON.toJSONString(headers) + ". params :" + JSON.toJSONString(params));String apiUrl = HttpUtil.getUrlWithParams(url, params);HttpGet httpGet = new HttpGet(apiUrl);if ( headers != null && headers.size() > 0 ) {for (Map.Entry<String, String> entry : headers.entrySet()) {httpGet.addHeader(entry.getKey(), entry.getValue());}}CloseableHttpResponse response = null;try {response = httpClient.execute(httpGet);if (response == null || response.getStatusLine() == null) {return null;}int statusCode = response.getStatusLine().getStatusCode();if ( statusCode == HttpStatus.SC_OK ) {HttpEntity entityRes = response.getEntity();if (entityRes != null) {return EntityUtils.toString(entityRes, HttpUtil.UTF_8);}}return null;} catch (IOException e) {logger.error(e.getMessage(), e);} finally {if ( response != null ) {try {response.close();} catch (IOException e) {}}}return null;}public String doPost(String apiUrl, Map<String, Object> params) {return this.doPost(apiUrl, Collections.emptyMap(), params);}public String doPost(String apiUrl,Map<String, String> headers,String jsonParam) throws UnsupportedEncodingException {logger.debug("doPost url:" + apiUrl + ". headers :" + JSON.toJSONString(headers) + ". jsonParam :" + jsonParam);HttpPost httpPost = new HttpPost(apiUrl);if ( headers != null && headers.size() > 0 ) {for (Map.Entry<String, String> entry : headers.entrySet()) {httpPost.addHeader(entry.getKey(), entry.getValue());}}if ( StrUtil.isNotBlank(jsonParam)) {StringEntity jsonEntity = new StringEntity(jsonParam);httpPost.setEntity(jsonEntity);httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");}CloseableHttpResponse response = null;try {response = httpClient.execute(httpPost);if (response == null || response.getStatusLine() == null) {return null;}int statusCode = response.getStatusLine().getStatusCode();if ( statusCode == HttpStatus.SC_OK ) {HttpEntity entityRes = response.getEntity();if ( entityRes != null ) {return EntityUtils.toString(entityRes, HttpUtil.UTF_8);}}return null;} catch (IOException e) {logger.error(e.getMessage(), e);} finally {if (response != null) {try {response.close();} catch (IOException e) {}}}return null;}public String doPost(String apiUrl, Map<String, String> headers, Map<String, Object> params) {logger.debug("doPost url:" + apiUrl + ". headers :" + JSON.toJSONString(headers) + ". params :" + JSON.toJSONString(params));HttpPost httpPost = new HttpPost(apiUrl);if ( headers != null && headers.size() > 0 ) {for (Map.Entry<String, String> entry : headers.entrySet()) {httpPost.addHeader(entry.getKey(), entry.getValue());}}if ( params != null && params.size() > 0 ) {HttpEntity entityReq = getUrlEncodedFormEntity(params);httpPost.setEntity(entityReq);}CloseableHttpResponse response = null;try {response = httpClient.execute(httpPost);if (response == null || response.getStatusLine() == null) {return null;}int statusCode = response.getStatusLine().getStatusCode();if ( statusCode == HttpStatus.SC_OK ) {HttpEntity entityRes = response.getEntity();if ( entityRes != null ) {return EntityUtils.toString(entityRes, HttpUtil.UTF_8);}}return null;} catch (IOException e) {logger.error(e.getMessage(), e);} finally {if (response != null) {try {response.close();} catch (IOException e) {}}}return null;}public String doDelete(String url, Map<String, String> headers, Map<String, Object> params) {logger.info("doDelete url:" + url + ". headers :" + JSON.toJSONString(headers) + ". params :" + JSON.toJSONString(params));HttpDelete httpDelete = new HttpDelete(url);if ( headers != null && headers.size() > 0 ) {for (Map.Entry<String, String> entry : headers.entrySet()) {httpDelete.addHeader(entry.getKey(), entry.getValue());}}CloseableHttpResponse response = null;try {response = httpClient.execute(httpDelete);if (response == null || response.getStatusLine() == null) {return null;}int statusCode = response.getStatusLine().getStatusCode();if ( statusCode == HttpStatus.SC_OK ) {HttpEntity entityRes = response.getEntity();if (entityRes != null) {return EntityUtils.toString(entityRes, HttpUtil.UTF_8);}}return null;} catch (IOException e) {logger.error(e.getMessage(), e);} finally {if ( response != null ) {try {response.close();} catch (IOException e) {}}}return null;}private HttpEntity getUrlEncodedFormEntity(Map<String, Object> params) {List<NameValuePair> pairList = new ArrayList<NameValuePair>(params.size());for (Map.Entry<String, Object> entry : params.entrySet()) {NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());pairList.add(pair);}return new UrlEncodedFormEntity(pairList, Charset.forName(HttpUtil.UTF_8));}public void shutdown() {idleThread.shutdown();}private class IdleConnectionMonitorThread extends Thread {private final HttpClientConnectionManager connMgr;private volatile boolean exitFlag = false;public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr) {this.connMgr = connMgr;setDaemon(true);}@Overridepublic void run() {while (!this.exitFlag) {synchronized (this) {try {this.wait(2000);} catch (InterruptedException e) {e.printStackTrace();}}connMgr.closeExpiredConnections();connMgr.closeIdleConnections(30, TimeUnit.SECONDS);}}public void shutdown() {this.exitFlag = true;synchronized (this) {notify();}}}}