HttpUtil.java 9.3 KB

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