123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- package api.common.util;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- 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.conn.ssl.NoopHostnameVerifier;
- import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
- import org.apache.http.entity.ContentType;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.entity.mime.HttpMultipartMode;
- import org.apache.http.entity.mime.MultipartEntityBuilder;
- import org.apache.http.entity.mime.content.FileBody;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.util.EntityUtils;
- import javax.net.ssl.SSLContext;
- import javax.net.ssl.TrustManager;
- import javax.net.ssl.X509TrustManager;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.nio.charset.StandardCharsets;
- import java.security.cert.X509Certificate;
- import java.util.Map;
- /**
- * <dependency>
- * <groupId>org.apache.httpcomponents</groupId>
- * <artifactId>httpclient</artifactId>
- * <version>${http.client.version}</version>
- * </dependency>
- */
- @Slf4j
- 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 CloseableHttpClient getHttpClient() {
- return getCloseableHttpClient();
- }
- private static CloseableHttpClient getCloseableHttpClient() {
- try {
- SSLContext sslContext = SSLContext.getInstance("TLS");
- X509TrustManager x509TrustManager = new X509TrustManager() {
- @Override
- public X509Certificate[] getAcceptedIssuers() {
- return null;
- }
- @Override
- public void checkClientTrusted(X509Certificate[] arg0, String arg1) {
- }
- @Override
- public void checkServerTrusted(X509Certificate[] arg0, String arg1) {
- }
- };
- sslContext.init(null, new TrustManager[]{x509TrustManager}, null);
- SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
- // PoolingHttpClientConnectionManager pool = new PoolingHttpClientConnectionManager();
- // pool.setMaxTotal(300);
- // pool.setDefaultMaxPerRoute(300);
- // return HttpClients.custom().setConnectionManager(pool).setSSLSocketFactory(sslConnectionSocketFactory).build();
- return HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).build();
- } catch (Exception exception) {
- throw new RuntimeException("获取 httpClient 失败。", exception);
- }
- }
- public static RequestConfig getRequestConfig() {
- return RequestConfig.custom()
- .setSocketTimeout(60000)
- .setConnectTimeout(60000)
- .setConnectionRequestTimeout(60000)
- .build();
- }
- /**
- * 发送本地文件
- *
- * @param url http请求路径
- * @param file 文件对象
- * @return 响应体
- */
- public static String post(String url, File file) {
- String result = null;
- try {
- CloseableHttpClient client = getCloseableHttpClient();
- RequestConfig config = getRequestConfig();
- //1 创建 post 请求
- HttpPost post = new HttpPost(url);
- post.setConfig(config);
- //2 设置请求体
- post.setEntity(MultipartEntityBuilder.create().setCharset(StandardCharsets.UTF_8).setMode(HttpMultipartMode.BROWSER_COMPATIBLE) //设置浏览器兼容模式
- .addPart("file", new FileBody(file, ContentType.MULTIPART_FORM_DATA)).build());
- //3 获取响应结果
- HttpResponse response = client.execute(post);
- HttpEntity responseEntity = response.getEntity();
- result = EntityUtils.toString(responseEntity);
- //4 关闭流
- EntityUtils.consume(responseEntity);
- return result;
- } catch (org.apache.http.conn.ConnectTimeoutException cte) {
- log.debug(url + " 请求超时。", cte);
- log.info(url + " 请求超时。");
- } catch (Exception e) {
- log.info("请求 " + url + " 失败。", e);
- }
- return result;
- }
- /**
- * 默认请求头的 get 请求,参数拼接到 url 上
- *
- * @param url 请求路径带参数
- * @return 请求结果
- */
- public static String get(String url) {
- log.info("发送HTTP请求:" + url);
- String result;
- try {
- //1 创建 GET 对象
- HttpGet get = new HttpGet(url);
- get.setConfig(getRequestConfig());
- //2 执行获取请求结果
- result = EntityUtils.toString(getCloseableHttpClient().execute(get).getEntity());
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- return result;
- }
- /**
- * 默认请求头的 get 请求
- *
- * @param closeableHttpClient http 客户端
- * @param requestConfig 请求配置
- * @param url 请求路径带参数
- * @return 请求结果
- */
- public static String get(CloseableHttpClient closeableHttpClient, RequestConfig requestConfig, String url) {
- log.info("发送HTTP请求:" + url);
- String result = null;
- try {
- //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) {
- result = EntityUtils.toString(response.getEntity());
- } else {
- log.error(response.getEntity().toString());
- throw new RuntimeException("请求错误:" + response);
- }
- } catch (org.apache.http.conn.ConnectTimeoutException cte) {
- log.info(url + " 请求超时。", cte);
- } catch (Exception e) {
- log.info("请求 " + url + " 失败。", e);
- }
- return result;
- }
- /**
- * 带请求头的 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);
- }
- }
- /**
- * 默认请求头的 get 请求下载文件
- *
- * @param url 请求地址
- * @return 文件流
- */
- public static InputStream getInputStream(String url) {
- try {
- log.info("发送请求:" + url);
- CloseableHttpClient closeableHttpClient = getCloseableHttpClient();
- RequestConfig requestConfig = getRequestConfig();
- return getInputStream(closeableHttpClient, requestConfig, url, null);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- /**
- * 默认请求头的 get 请求下载文件
- *
- * @param url 请求地址
- * @return 文件流
- * @throws IOException 异常
- */
- public static InputStream getInputStream(CloseableHttpClient closeableHttpClient, RequestConfig requestConfig, String url) throws IOException {
- return getInputStream(closeableHttpClient, requestConfig, url, null);
- }
- /**
- * 自定义请求头的 get 请求下载文件
- *
- * @param url 请求地址
- * @param headers 请求头,可为 null
- * @return 文件流
- * @throws IOException 异常
- */
- public static InputStream getInputStream(CloseableHttpClient closeableHttpClient, RequestConfig requestConfig, String url, Map<String, String> headers) throws IOException {
- InputStream result = null;
- try {
- //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();
- log.info("请求 " + url + " 状态码为:" + statusCode);
- if (statusCode == 200) {
- result = response.getEntity().getContent();
- }
- } catch (Exception e) {
- log.info("请求 " + url + " 失败。", e);
- }
- return result;
- }
- /**
- * 自定义请求头的 post 请求
- */
- public static String post(String url, Map<String, String> headers, Map<String, String> params){
- log.info("发送HTTP请求:" + url+",请求参数:"+params+",请求头:"+headers);
- try {
- CloseableHttpClient closeableHttpClient = getCloseableHttpClient();
- RequestConfig requestConfig = getRequestConfig();
- //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 设置请求体
- post.addHeader("Content-Type", "application/json");
- if (!CollectionUtil.isEmpty(params)) {
- post.setEntity(new StringEntity(JsonUtil.beanToJson(params), "utf-8"));
- }
- //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);
- }
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- /**
- * 自定义请求头的 post 请求
- */
- public static String post(CloseableHttpClient closeableHttpClient, RequestConfig requestConfig, 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 设置请求体
- post.addHeader("Content-Type", "application/json");
- if (!CollectionUtil.isEmpty(params)) {
- post.setEntity(new StringEntity(JsonUtil.beanToJson(params), "utf-8"));
- }
- //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);
- // }
- // }
- }
|