当前位置: 代码迷 >> 综合 >> Utils-HttpsClientUtil
  详细解决方案

Utils-HttpsClientUtil

热度:17   发布时间:2024-01-09 15:52:53.0

一、http封装工具类

  • 忽略证书https请求

  • 支持rest风格请求与传参

  • 支持header请求头封装

  • 支持Query与HttpEntity传参

maven依赖

<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpmime</artifactId><version>4.5.9</version>
</dependency>
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.9</version>
</dependency>
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.4.11</version>
</dependency>
  • 工具类

package com.ruoyi.utils;import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.*;
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.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.TrustStrategy;
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.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;/*** HttpClient 工具类 by ChenYb data 2020-02-16*/
public class HttpClientUtil {private static final Logger logger = LoggerFactory.getLogger( HttpClientUtil.class);private static final String HTTP = "http";private static final String HTTPS = "https";private static SSLConnectionSocketFactory sslConnectionSocketFactory = null;private static PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = null;//连接池管理类private static SSLContextBuilder sslContextBuilder = null;//管理Https连接的上下文类/*** 证书忽略部分*/static {try {sslContextBuilder = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {@Overridepublic boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
//                    信任所有站点 直接返回truereturn true;}});sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build(), new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE);Registry<ConnectionSocketFactory> registryBuilder = RegistryBuilder.<ConnectionSocketFactory>create().register(HTTP, new PlainConnectionSocketFactory()).register(HTTPS, sslConnectionSocketFactory).build();poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(registryBuilder);poolingHttpClientConnectionManager.setMaxTotal(200);} catch (NoSuchAlgorithmException e) {e.printStackTrace();} catch (KeyStoreException e) {e.printStackTrace();} catch (KeyManagementException e) {e.printStackTrace();}}/*** 获取连接** @return* @throws Exception*/public static CloseableHttpClient getHttpClinet() throws Exception {CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).setConnectionManager(poolingHttpClientConnectionManager).setConnectionManagerShared(true).build();return httpClient;}/*** 发送post请求** @param url:请求地址* @param header:请求头参数* @param params:表单参数  form提交* @param httpEntity   json/xml参数* @return*/public static String doPostRequest(String url, Map<String, String> header, Map<String, String> params, HttpEntity httpEntity) {String resultStr = "";if (StringUtils.isEmpty(url)) {return resultStr;}CloseableHttpClient httpClient = null;CloseableHttpResponse httpResponse = null;try {httpClient = HttpClientUtil.getHttpClinet();HttpPost httpPost = new HttpPost(url);//请求头header信息if (MapUtils.isNotEmpty(header)) {for (Map.Entry<String, String> stringStringEntry : header.entrySet()) {httpPost.setHeader(stringStringEntry.getKey(), stringStringEntry.getValue());}}//请求参数信息if (MapUtils.isNotEmpty(params)) {List<NameValuePair> paramList = new ArrayList<>();for (Map.Entry<String, String> stringStringEntry : params.entrySet()) {paramList.add(new BasicNameValuePair(stringStringEntry.getKey(), stringStringEntry.getValue()));}UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(paramList, Consts.UTF_8);httpPost.setEntity(urlEncodedFormEntity);}//实体设置if (httpEntity != null) {httpPost.setEntity(httpEntity);}//发起请求httpResponse = httpClient.execute(httpPost);int statusCode = httpResponse.getStatusLine().getStatusCode();if (statusCode == HttpStatus.SC_OK) {HttpEntity httpResponseEntity = httpResponse.getEntity();resultStr = EntityUtils.toString(httpResponseEntity);logger.info("请求正常,请求地址:{},响应结果:{}", url, resultStr);} else {StringBuffer stringBuffer = new StringBuffer();HeaderIterator headerIterator = httpResponse.headerIterator();while (headerIterator.hasNext()) {stringBuffer.append("\t" + headerIterator.next());}logger.info("异常信息:请求地址:{},响应状态:{},请求返回结果:{}", url, httpResponse.getStatusLine().getStatusCode(), stringBuffer);}} catch (Exception e) {e.printStackTrace();} finally {HttpClientUtil.closeConnection(httpClient, httpResponse);}return resultStr;}public static String doGetRequest(String url, Map<String, String> header, Map<String, String> params) {String resultStr = "";if (StringUtils.isEmpty(url)) {return resultStr;}CloseableHttpClient httpClient = null;CloseableHttpResponse httpResponse = null;try {httpClient = HttpClientUtil.getHttpClinet();//请求参数信息if (MapUtils.isNotEmpty(params)) {url = url + buildUrl(params);}HttpGet httpGet = new HttpGet(url);RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000)//连接超时.setConnectionRequestTimeout(5000)//请求超时.setSocketTimeout(5000)//套接字连接超时.setRedirectsEnabled(true).build();//允许重定向httpGet.setConfig(requestConfig);if (MapUtils.isNotEmpty(header)) {for (Map.Entry<String, String> stringStringEntry : header.entrySet()) {httpGet.setHeader(stringStringEntry.getKey(), stringStringEntry.getValue());}}//发起请求httpResponse = httpClient.execute(httpGet);int statusCode = httpResponse.getStatusLine().getStatusCode();if (statusCode == HttpStatus.SC_OK) {resultStr = EntityUtils.toString(httpResponse.getEntity(), Consts.UTF_8);logger.info("请求地址:{},响应结果:{}", url, resultStr);} else {StringBuffer stringBuffer = new StringBuffer();HeaderIterator headerIterator = httpResponse.headerIterator();while (headerIterator.hasNext()) {stringBuffer.append("\t" + headerIterator.next());}logger.info("异常信息:请求响应状态:{},请求返回结果:{}", httpResponse.getStatusLine().getStatusCode(), stringBuffer);}} catch (Exception e) {e.printStackTrace();} finally {HttpClientUtil.closeConnection(httpClient, httpResponse);}return resultStr;}/*** 关掉连接释放资源*/private static void closeConnection(CloseableHttpClient httpClient, CloseableHttpResponse httpResponse) {if (httpClient != null) {try {httpClient.close();} catch (IOException e) {e.printStackTrace();}}if (httpResponse != null) {try {httpResponse.close();} catch (IOException e) {e.printStackTrace();}}}/*** 构造get请求的参数** @return*/private static String buildUrl(Map<String, String> map) {if (MapUtils.isEmpty(map)) {return "";}StringBuffer stringBuffer = new StringBuffer("?");for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {stringBuffer.append(stringStringEntry.getKey()).append("=").append(stringStringEntry.getValue()).append("&");}String result = stringBuffer.toString();if (StringUtils.isNotEmpty(result)) {result = result.substring(0, result.length() - 1);//去掉结尾的&连接符}return result;}
}
  • 使用说明 
    • header与params直接封装Map即可
    • HttpEntity 使用方式 new StringEntity("JSON格式字符串,将实体直接转换为JSON数据即可",“UTF-8,指定编码格式即可”)
    • 集合或者数组,可以启用 IdentityHashMap 传值

Mr.Chenyb 随笔记录,方便自己使用