package com.utils;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
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.SSLConnectionSocketFactory;
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.util.EntityUtils;
import tw.tool.helper.LogHelper;
public class httpsApi {
private static RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(100000).setConnectTimeout(15000)
.setConnectionRequestTimeout(15000).build();
/**
* Https请求处理
* @param httpPost
* @param isHttps
* @return
*/
private static String sendHttpPost(HttpPost httpPost,boolean isHttps) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
String responseContent = null;
httpClient=(CloseableHttpClient)getHttpClient(isHttps);
httpPost.setConfig(requestConfig);
// 执行请求
try {
response = httpClient.execute(httpPost);
entity = response.getEntity();
LogHelper.info("调用获取用户信息接口返回:"+ entity.toString());
responseContent = EntityUtils.toString(entity, "UTF-8");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
// 关闭连接,释放资源
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return responseContent;
}
/**
* HttpClient创建,根据情况创建请求方式
* @param isHttps
* @return
*/
private static HttpClient getHttpClient(boolean isHttps) {
if (isHttps) {
SSLContext sslContext = createIgnoreVerifySSL();
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", new SSLConnectionSocketFactory(sslContext)).build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(
socketFactoryRegistry);
return HttpClients.custom().setConnectionManager(connManager).build();
} else {
return HttpClients.createDefault();
}
}
/**
* HTTPS请求证书处理
*
* @return
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
private static SSLContext createIgnoreVerifySSL() {
try {
SSLContext sc = SSLContext.getInstance("SSL");
X509TrustManager trustManager = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String paramString) {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String paramString) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sc.init(null, new TrustManager[] { trustManager }, null);
return sc;
} catch (KeyManagementException e) {
throw new RuntimeException(e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
}
原文:https://www.cnblogs.com/lifan12589/p/11719837.html