发送https请求
package com.xzgx.thirdparty.util;
import com.xzgx.common.exception.CustomException;
import com.xzgx.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Map;
/**
* @author jinxionghui
* @date 2022/1/21
*/
@Slf4j
public class HttpsClient {
public static final String charset = "utf-8";
/**
* 发送json格式http请求
*
* @param url
* @param jsonStr
* @param charset
* @param headerMap
* @return
*/
public static String doPost(String url, String jsonStr, String charset, Map<String, String> headerMap) {
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try {
httpClient = createSSLClientDefault();
httpPost = new HttpPost(url);
//设置参数
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
for (String k : headerMap.keySet()) {
httpPost.addHeader(k, headerMap.get(k));
}
StringEntity stringEntity = new StringEntity(jsonStr);
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
HttpResponse response = httpClient.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, charset);
}
}
} catch (ClientProtocolException e) {
log.error("客户端协议错误!", e);
throw new CustomException("客户端协议错误!", e);
} catch (UnsupportedEncodingException e) {
log.error("不支持字符编码!", e);
throw new CustomException("不支持字符编码!", e);
} catch (IOException e) {
log.error("io异常!", e);
throw new CustomException("io异常!", e);
} catch (Exception e) {
log.error("系统异常!", e);
throw new CustomException("系统异常!", e);
}
if (StringUtils.isBlank(result)) {
log.error("https调用返回为空!");
throw new CustomException("系统异常!");
}
return result;
}
/**
* 发送json格式http请求
*
* @param url
* @param map
* @param charset
* @param headerMap
* @return
*/
public static String doGet(String url, String charset, Map<String, String> headerMap) {
HttpClient httpClient = null;
HttpGet httpPost = null;
String result = null;
try {
httpClient = createSSLClientDefault();
httpPost = new HttpGet(url);
//设置参数
httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
for (String k : headerMap.keySet()) {
httpPost.addHeader(k, headerMap.get(k));
}
HttpResponse response = httpClient.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, charset);
}
}
} catch (ClientProtocolException e) {
log.error("客户端协议错误!", e);
throw new CustomException("客户端协议错误!", e);
} catch (UnsupportedEncodingException e) {
log.error("不支持字符编码!", e);
throw new CustomException("不支持字符编码!", e);
} catch (IOException e) {
log.error("io异常!", e);
throw new CustomException("io异常!", e);
} catch (Exception e) {
log.error("系统异常!", e);
throw new CustomException("系统异常!", e);
}
return result;
}
public static CloseableHttpClient createSSLClientDefault() {
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(
null, new TrustStrategy() {
// 信任所有
@Override
public boolean isTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslContext);
//有配置代码的情况使用,如果使用系统代理的方式,部署服务器可能会报403
return HttpClientBuilder.create().useSystemProperties().setSSLSocketFactory(sslsf).build();
//无配置代码的情况使用
//return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (Exception e) {
log.error("系统异常!", e);
}
return HttpClients.createDefault();
}
}```
|