123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- 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;
- /**
- * <dependency>
- * <groupId>org.apache.httpcomponents</groupId>
- * <artifactId>httpclient</artifactId>
- * <version>${http.client.version}</version>
- * </dependency>
- */
- 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<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>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<String, String> 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<String, String> headers,
- Map<String, String> 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<String, String> 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<String, String> headers, Map<String, String> 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);
- // }
- // }
- }
|