package api.common.util;
import org.apache.http.client.config.RequestConfig;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
/**
*
* org.apache.httpcomponents
* httpclient
* ${http.client.version}
*
*/
public class HttpUtil {
public static CloseableHttpClient getHttpClient() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
PlainConnectionSocketFactory plainConnectionSocketFactory = new PlainConnectionSocketFactory();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
SSLContextBuilder.create().loadTrustMaterial(null, (x509Certificates, s) -> true).build(), // 全部信任 不做身份鉴定
new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"},
null,
NoopHostnameVerifier.INSTANCE);
Registry registry = RegistryBuilder.create()
.register("http", plainConnectionSocketFactory)
.register("https", sslConnectionSocketFactory)
.build();
PoolingHttpClientConnectionManager pool = new PoolingHttpClientConnectionManager(registry);
pool.setMaxTotal(300);
pool.setDefaultMaxPerRoute(300);
return HttpClients.custom().setConnectionManager(pool).build();
}
public static RequestConfig getRequestConfig() {
return RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setRedirectsEnabled(false)
.setExpectContinueEnabled(false)
.build();
}
/**
* 普通 get 请求
*
* @param closeableHttpClient http 客户端
* @param requestConfig 请求配置
* @param url 请求路径带参数
* @return 请求结果
* @throws IOException IO异常
*/
public static String get(CloseableHttpClient closeableHttpClient, RequestConfig requestConfig, String url) throws IOException {
//1 创建 post 请求
HttpGet get = new HttpGet(url);
get.setConfig(requestConfig);
//2 发送请求
CloseableHttpResponse response = closeableHttpClient.execute(get);
//3 处理返回结果,如果状态码为200,就是正常返回
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
return EntityUtils.toString(response.getEntity());
} else {
throw new RuntimeException("------- 请求错误:" + statusCode);
}
}
/**
* 带请求头的 get 请求
*
* @param closeableHttpClient http 客户端
* @param requestConfig 请求配置
* @param url 请求路径带参数
* @param headers 请求头
* @return 请求结果
* @throws IOException IO异常
*/
public static String get(CloseableHttpClient closeableHttpClient, RequestConfig requestConfig, String url,
Map headers) throws IOException {
//1 创建 post 请求
HttpGet get = new HttpGet(url);
//2 设置请求默认配置
get.setConfig(requestConfig);
//3 设置请求头 post.setHeader("Content-type", "application/json; charset=utf-8");
if (!CollectionUtil.isEmpty(headers)) {
headers.forEach(get::setHeader);
}
//4 发送请求
CloseableHttpResponse response = closeableHttpClient.execute(get);
//5 处理返回结果,
//5-1 如果状态码为200,就是正常返回
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
return EntityUtils.toString(response.getEntity());
} else {
throw new RuntimeException("------- 请求错误:" + statusCode);
}
}
/**
* 发送 post 请求
*/
public static String post(
CloseableHttpClient closeableHttpClient,
RequestConfig requestConfig,
String url,
Map headers,
Map params
) throws Exception {
//1 创建 post 请求
HttpPost post = new HttpPost(url);
//2 设置请求默认配置
post.setConfig(requestConfig);
//3 设置请求头 post.setHeader("Content-type", "application/json; charset=utf-8");
if (!CollectionUtil.isEmpty(headers)) {
headers.forEach(post::setHeader);
}
//4 设置请求体
if (!CollectionUtil.isEmpty(params)) {
params.forEach(post::setHeader);
}
//5 发送请求
CloseableHttpResponse response = closeableHttpClient.execute(post);
//6 处理返回结果,
//6-1 如果状态码为200,就是正常返回
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
return EntityUtils.toString(response.getEntity());
} else {
throw new RuntimeException("------- 请求错误:" + statusCode);
}
}
//
// /**
// * 通过 get 请求下载文件
// * 发送 get 请求,get 请求的参数在 url 里
// *
// * @param url 请求地址
// * @param headers 请求头,可为 null
// * @return 文件流
// * @throws IOException 异常
// */
// public static InputStream getDownload(String url, Map headers) throws IOException {
// //1 创建 post 请求
// HttpGet get = new HttpGet(url);
// //2 设置请求默认配置
// get.setConfig(requestConfig);
// //3 设置请求头 post.setHeader("Content-type", "application/json; charset=utf-8");
// if (!CollectionUtil.isEmpty(headers)) {
// headers.forEach(get::setHeader);
// }
// //4 发送请求
// CloseableHttpResponse response = httpClient.execute(get);
// //5 处理返回结果,
// //5-1 如果状态码为200,就是正常返回
// int statusCode = response.getStatusLine().getStatusCode();
// if (statusCode == 200) {
// return response.getEntity().getContent();
// } else {
// throw new RuntimeException("------- 请求错误:" + statusCode);
// }
// }
//
//
// /**
// * 发送 post 请求
// */
// public static InputStream postUpload(String url) throws IOException {
// //1 创建 post 请求
// HttpPost post = new HttpPost(url);
// //2 设置请求默认配置
// post.setConfig(requestConfig);
// //3 设置请求头 post.setHeader("Content-type", "application/json; charset=utf-8");
// post.setHeader("Content-type", "multipart/form-data");
// //5 发送请求
// CloseableHttpResponse response = httpClient.execute(post);
// //6 处理返回结果,
// //6-1 如果状态码为 200,就是正常返回
// int statusCode = response.getStatusLine().getStatusCode();
// if (statusCode == 200) {
// return response.getEntity().getContent();
// } else {
// throw new RuntimeException("------- 请求错误:" + statusCode);
// }
// }
//
// /**
// * 发送 post 请求
// */
// public static InputStream postDownload(String url, Map headers, Map params) throws IOException {
// //1 创建 post 请求
// HttpPost post = new HttpPost(url);
// //2 设置请求默认配置
// post.setConfig(requestConfig);
// //3 设置请求头 post.setHeader("Content-type", "application/json; charset=utf-8");
// if (!CollectionUtil.isEmpty(headers)) {
// headers.forEach(post::setHeader);
// }
// //4 设置请求体
// if (!CollectionUtil.isEmpty(params)) {
// params.forEach(post::setHeader);
// }
// //5 发送请求
// CloseableHttpResponse response = httpClient.execute(post);
// //6 处理返回结果,
// //6-1 如果状态码为 200,就是正常返回
// int statusCode = response.getStatusLine().getStatusCode();
// if (statusCode == 200) {
// return response.getEntity().getContent();
// } else {
// throw new RuntimeException("------- 请求错误:" + statusCode);
// }
// }
}