HttpUtil.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package api.common.util;
  2. import org.apache.http.client.config.RequestConfig;
  3. import org.apache.http.client.methods.CloseableHttpResponse;
  4. import org.apache.http.client.methods.HttpGet;
  5. import org.apache.http.client.methods.HttpPost;
  6. import org.apache.http.config.Registry;
  7. import org.apache.http.config.RegistryBuilder;
  8. import org.apache.http.conn.socket.ConnectionSocketFactory;
  9. import org.apache.http.conn.socket.PlainConnectionSocketFactory;
  10. import org.apache.http.conn.ssl.NoopHostnameVerifier;
  11. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  12. import org.apache.http.impl.client.CloseableHttpClient;
  13. import org.apache.http.impl.client.HttpClients;
  14. import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
  15. import org.apache.http.ssl.SSLContextBuilder;
  16. import org.apache.http.util.EntityUtils;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.security.KeyManagementException;
  20. import java.security.KeyStoreException;
  21. import java.security.NoSuchAlgorithmException;
  22. import java.util.Map;
  23. /**
  24. * <dependency>
  25. * <groupId>org.apache.httpcomponents</groupId>
  26. * <artifactId>httpclient</artifactId>
  27. * <version>${http.client.version}</version>
  28. * </dependency>
  29. */
  30. public class HttpUtil {
  31. public static CloseableHttpClient getHttpClient() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
  32. PlainConnectionSocketFactory plainConnectionSocketFactory = new PlainConnectionSocketFactory();
  33. SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
  34. SSLContextBuilder.create().loadTrustMaterial(null, (x509Certificates, s) -> true).build(), // 全部信任 不做身份鉴定
  35. new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"},
  36. null,
  37. NoopHostnameVerifier.INSTANCE);
  38. Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
  39. .register("http", plainConnectionSocketFactory)
  40. .register("https", sslConnectionSocketFactory)
  41. .build();
  42. PoolingHttpClientConnectionManager pool = new PoolingHttpClientConnectionManager(registry);
  43. pool.setMaxTotal(300);
  44. pool.setDefaultMaxPerRoute(300);
  45. return HttpClients.custom().setConnectionManager(pool).build();
  46. }
  47. public static RequestConfig getRequestConfig() {
  48. return RequestConfig.custom()
  49. .setSocketTimeout(5000)
  50. .setConnectTimeout(5000)
  51. .setConnectionRequestTimeout(5000)
  52. .setRedirectsEnabled(false)
  53. .setExpectContinueEnabled(false)
  54. .build();
  55. }
  56. /**
  57. * 普通 get 请求
  58. *
  59. * @param closeableHttpClient http 客户端
  60. * @param requestConfig 请求配置
  61. * @param url 请求路径带参数
  62. * @return 请求结果
  63. * @throws IOException IO异常
  64. */
  65. public static String get(CloseableHttpClient closeableHttpClient, RequestConfig requestConfig, String url) throws IOException {
  66. //1 创建 post 请求
  67. HttpGet get = new HttpGet(url);
  68. get.setConfig(requestConfig);
  69. //2 发送请求
  70. CloseableHttpResponse response = closeableHttpClient.execute(get);
  71. //3 处理返回结果,如果状态码为200,就是正常返回
  72. int statusCode = response.getStatusLine().getStatusCode();
  73. if (statusCode == 200) {
  74. return EntityUtils.toString(response.getEntity());
  75. } else {
  76. throw new RuntimeException("------- 请求错误:" + statusCode);
  77. }
  78. }
  79. /**
  80. * 带请求头的 get 请求
  81. *
  82. * @param closeableHttpClient http 客户端
  83. * @param requestConfig 请求配置
  84. * @param url 请求路径带参数
  85. * @param headers 请求头
  86. * @return 请求结果
  87. * @throws IOException IO异常
  88. */
  89. public static String get(CloseableHttpClient closeableHttpClient, RequestConfig requestConfig, String url,
  90. Map<String, String> headers) throws IOException {
  91. //1 创建 post 请求
  92. HttpGet get = new HttpGet(url);
  93. //2 设置请求默认配置
  94. get.setConfig(requestConfig);
  95. //3 设置请求头 post.setHeader("Content-type", "application/json; charset=utf-8");
  96. if (!CollectionUtil.isEmpty(headers)) {
  97. headers.forEach(get::setHeader);
  98. }
  99. //4 发送请求
  100. CloseableHttpResponse response = closeableHttpClient.execute(get);
  101. //5 处理返回结果,
  102. //5-1 如果状态码为200,就是正常返回
  103. int statusCode = response.getStatusLine().getStatusCode();
  104. if (statusCode == 200) {
  105. return EntityUtils.toString(response.getEntity());
  106. } else {
  107. throw new RuntimeException("------- 请求错误:" + statusCode);
  108. }
  109. }
  110. /**
  111. * 默认请求头的 get 请求下载文件
  112. *
  113. * @param url 请求地址
  114. * @return 文件流
  115. * @throws IOException 异常
  116. */
  117. public static InputStream getInputStream(CloseableHttpClient closeableHttpClient, RequestConfig requestConfig, String url) throws IOException {
  118. return getInputStream(closeableHttpClient, requestConfig, url, null);
  119. }
  120. /**
  121. * 自定义请求头的 get 请求下载文件
  122. *
  123. * @param url 请求地址
  124. * @param headers 请求头,可为 null
  125. * @return 文件流
  126. * @throws IOException 异常
  127. */
  128. public static InputStream getInputStream(CloseableHttpClient closeableHttpClient, RequestConfig requestConfig, String url, Map<String, String> headers) throws IOException {
  129. //1 创建 post 请求
  130. HttpGet get = new HttpGet(url);
  131. //2 设置请求默认配置
  132. get.setConfig(requestConfig);
  133. //3 设置请求头 post.setHeader("Content-type", "application/json; charset=utf-8");
  134. if (!CollectionUtil.isEmpty(headers)) {
  135. headers.forEach(get::setHeader);
  136. }
  137. //4 发送请求
  138. CloseableHttpResponse response = closeableHttpClient.execute(get);
  139. //5 处理返回结果,
  140. //5-1 如果状态码为200,就是正常返回
  141. int statusCode = response.getStatusLine().getStatusCode();
  142. if (statusCode == 200) {
  143. return response.getEntity().getContent();
  144. } else {
  145. throw new RuntimeException("------- 请求错误:" + statusCode);
  146. }
  147. }
  148. /**
  149. * 发送 post 请求
  150. */
  151. public static String post(
  152. CloseableHttpClient closeableHttpClient,
  153. RequestConfig requestConfig,
  154. String url,
  155. Map<String, String> headers,
  156. Map<String, String> params
  157. ) throws Exception {
  158. //1 创建 post 请求
  159. HttpPost post = new HttpPost(url);
  160. //2 设置请求默认配置
  161. post.setConfig(requestConfig);
  162. //3 设置请求头 post.setHeader("Content-type", "application/json; charset=utf-8");
  163. if (!CollectionUtil.isEmpty(headers)) {
  164. headers.forEach(post::setHeader);
  165. }
  166. //4 设置请求体
  167. if (!CollectionUtil.isEmpty(params)) {
  168. params.forEach(post::setHeader);
  169. }
  170. //5 发送请求
  171. CloseableHttpResponse response = closeableHttpClient.execute(post);
  172. //6 处理返回结果,
  173. //6-1 如果状态码为200,就是正常返回
  174. int statusCode = response.getStatusLine().getStatusCode();
  175. if (statusCode == 200) {
  176. return EntityUtils.toString(response.getEntity());
  177. } else {
  178. throw new RuntimeException("------- 请求错误:" + statusCode);
  179. }
  180. }
  181. //
  182. // /**
  183. // * 通过 get 请求下载文件
  184. // * 发送 get 请求,get 请求的参数在 url 里
  185. // *
  186. // * @param url 请求地址
  187. // * @param headers 请求头,可为 null
  188. // * @return 文件流
  189. // * @throws IOException 异常
  190. // */
  191. // public static InputStream getDownload(String url, Map<String, String> headers) throws IOException {
  192. // //1 创建 post 请求
  193. // HttpGet get = new HttpGet(url);
  194. // //2 设置请求默认配置
  195. // get.setConfig(requestConfig);
  196. // //3 设置请求头 post.setHeader("Content-type", "application/json; charset=utf-8");
  197. // if (!CollectionUtil.isEmpty(headers)) {
  198. // headers.forEach(get::setHeader);
  199. // }
  200. // //4 发送请求
  201. // CloseableHttpResponse response = httpClient.execute(get);
  202. // //5 处理返回结果,
  203. // //5-1 如果状态码为200,就是正常返回
  204. // int statusCode = response.getStatusLine().getStatusCode();
  205. // if (statusCode == 200) {
  206. // return response.getEntity().getContent();
  207. // } else {
  208. // throw new RuntimeException("------- 请求错误:" + statusCode);
  209. // }
  210. // }
  211. //
  212. //
  213. // /**
  214. // * 发送 post 请求
  215. // */
  216. // public static InputStream postUpload(String url) throws IOException {
  217. // //1 创建 post 请求
  218. // HttpPost post = new HttpPost(url);
  219. // //2 设置请求默认配置
  220. // post.setConfig(requestConfig);
  221. // //3 设置请求头 post.setHeader("Content-type", "application/json; charset=utf-8");
  222. // post.setHeader("Content-type", "multipart/form-data");
  223. // //5 发送请求
  224. // CloseableHttpResponse response = httpClient.execute(post);
  225. // //6 处理返回结果,
  226. // //6-1 如果状态码为 200,就是正常返回
  227. // int statusCode = response.getStatusLine().getStatusCode();
  228. // if (statusCode == 200) {
  229. // return response.getEntity().getContent();
  230. // } else {
  231. // throw new RuntimeException("------- 请求错误:" + statusCode);
  232. // }
  233. // }
  234. //
  235. // /**
  236. // * 发送 post 请求
  237. // */
  238. // public static InputStream postDownload(String url, Map<String, String> headers, Map<String, String> params) throws IOException {
  239. // //1 创建 post 请求
  240. // HttpPost post = new HttpPost(url);
  241. // //2 设置请求默认配置
  242. // post.setConfig(requestConfig);
  243. // //3 设置请求头 post.setHeader("Content-type", "application/json; charset=utf-8");
  244. // if (!CollectionUtil.isEmpty(headers)) {
  245. // headers.forEach(post::setHeader);
  246. // }
  247. // //4 设置请求体
  248. // if (!CollectionUtil.isEmpty(params)) {
  249. // params.forEach(post::setHeader);
  250. // }
  251. // //5 发送请求
  252. // CloseableHttpResponse response = httpClient.execute(post);
  253. // //6 处理返回结果,
  254. // //6-1 如果状态码为 200,就是正常返回
  255. // int statusCode = response.getStatusLine().getStatusCode();
  256. // if (statusCode == 200) {
  257. // return response.getEntity().getContent();
  258. // } else {
  259. // throw new RuntimeException("------- 请求错误:" + statusCode);
  260. // }
  261. // }
  262. }