HttpUtils.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package com.ruoyi.common.utils.http;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.net.ConnectException;
  8. import java.net.SocketTimeoutException;
  9. import java.net.URL;
  10. import java.net.URLConnection;
  11. import java.nio.charset.StandardCharsets;
  12. import java.security.cert.X509Certificate;
  13. import javax.net.ssl.HostnameVerifier;
  14. import javax.net.ssl.HttpsURLConnection;
  15. import javax.net.ssl.SSLContext;
  16. import javax.net.ssl.SSLSession;
  17. import javax.net.ssl.TrustManager;
  18. import javax.net.ssl.X509TrustManager;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import com.ruoyi.common.constant.Constants;
  22. import com.ruoyi.common.utils.StringUtils;
  23. /**
  24. * 通用http发送方法
  25. *
  26. * @author ruoyi
  27. */
  28. public class HttpUtils
  29. {
  30. private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);
  31. /**
  32. * 向指定 URL 发送GET方法的请求
  33. *
  34. * @param url 发送请求的 URL
  35. * @return 所代表远程资源的响应结果
  36. */
  37. public static String sendGet(String url)
  38. {
  39. return sendGet(url, StringUtils.EMPTY);
  40. }
  41. /**
  42. * 向指定 URL 发送GET方法的请求
  43. *
  44. * @param url 发送请求的 URL
  45. * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
  46. * @return 所代表远程资源的响应结果
  47. */
  48. public static String sendGet(String url, String param)
  49. {
  50. return sendGet(url, param, Constants.UTF8);
  51. }
  52. /**
  53. * 向指定 URL 发送GET方法的请求
  54. *
  55. * @param url 发送请求的 URL
  56. * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
  57. * @param contentType 编码类型
  58. * @return 所代表远程资源的响应结果
  59. */
  60. public static String sendGet(String url, String param, String contentType)
  61. {
  62. StringBuilder result = new StringBuilder();
  63. BufferedReader in = null;
  64. try
  65. {
  66. String urlNameString = StringUtils.isNotBlank(param) ? url + "?" + param : url;
  67. log.info("sendGet - {}", urlNameString);
  68. URL realUrl = new URL(urlNameString);
  69. URLConnection connection = realUrl.openConnection();
  70. connection.setRequestProperty("accept", "*/*");
  71. connection.setRequestProperty("connection", "Keep-Alive");
  72. connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
  73. connection.connect();
  74. in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
  75. String line;
  76. while ((line = in.readLine()) != null)
  77. {
  78. result.append(line);
  79. }
  80. log.info("recv - {}", result);
  81. }
  82. catch (ConnectException e)
  83. {
  84. log.error("调用HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e);
  85. }
  86. catch (SocketTimeoutException e)
  87. {
  88. log.error("调用HttpUtils.sendGet SocketTimeoutException, url=" + url + ",param=" + param, e);
  89. }
  90. catch (IOException e)
  91. {
  92. log.error("调用HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e);
  93. }
  94. catch (Exception e)
  95. {
  96. log.error("调用HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e);
  97. }
  98. finally
  99. {
  100. try
  101. {
  102. if (in != null)
  103. {
  104. in.close();
  105. }
  106. }
  107. catch (Exception ex)
  108. {
  109. log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
  110. }
  111. }
  112. return result.toString();
  113. }
  114. /**
  115. * 向指定 URL 发送POST方法的请求
  116. *
  117. * @param url 发送请求的 URL
  118. * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
  119. * @return 所代表远程资源的响应结果
  120. */
  121. public static String sendPost(String url, String param)
  122. {
  123. PrintWriter out = null;
  124. BufferedReader in = null;
  125. StringBuilder result = new StringBuilder();
  126. try
  127. {
  128. log.info("sendPost - {}", url);
  129. URL realUrl = new URL(url);
  130. URLConnection conn = realUrl.openConnection();
  131. conn.setRequestProperty("accept", "*/*");
  132. conn.setRequestProperty("connection", "Keep-Alive");
  133. conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
  134. conn.setRequestProperty("Accept-Charset", "utf-8");
  135. conn.setRequestProperty("contentType", "utf-8");
  136. conn.setDoOutput(true);
  137. conn.setDoInput(true);
  138. out = new PrintWriter(conn.getOutputStream());
  139. out.print(param);
  140. out.flush();
  141. in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
  142. String line;
  143. while ((line = in.readLine()) != null)
  144. {
  145. result.append(line);
  146. }
  147. log.info("recv - {}", result);
  148. }
  149. catch (ConnectException e)
  150. {
  151. log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
  152. }
  153. catch (SocketTimeoutException e)
  154. {
  155. log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e);
  156. }
  157. catch (IOException e)
  158. {
  159. log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e);
  160. }
  161. catch (Exception e)
  162. {
  163. log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e);
  164. }
  165. finally
  166. {
  167. try
  168. {
  169. if (out != null)
  170. {
  171. out.close();
  172. }
  173. if (in != null)
  174. {
  175. in.close();
  176. }
  177. }
  178. catch (IOException ex)
  179. {
  180. log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
  181. }
  182. }
  183. return result.toString();
  184. }
  185. public static String sendSSLPost(String url, String param)
  186. {
  187. StringBuilder result = new StringBuilder();
  188. String urlNameString = url + "?" + param;
  189. try
  190. {
  191. log.info("sendSSLPost - {}", urlNameString);
  192. SSLContext sc = SSLContext.getInstance("SSL");
  193. sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
  194. URL console = new URL(urlNameString);
  195. HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
  196. conn.setRequestProperty("accept", "*/*");
  197. conn.setRequestProperty("connection", "Keep-Alive");
  198. conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
  199. conn.setRequestProperty("Accept-Charset", "utf-8");
  200. conn.setRequestProperty("contentType", "utf-8");
  201. conn.setDoOutput(true);
  202. conn.setDoInput(true);
  203. conn.setSSLSocketFactory(sc.getSocketFactory());
  204. conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
  205. conn.connect();
  206. InputStream is = conn.getInputStream();
  207. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  208. String ret = "";
  209. while ((ret = br.readLine()) != null)
  210. {
  211. if (ret != null && !"".equals(ret.trim()))
  212. {
  213. result.append(new String(ret.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
  214. }
  215. }
  216. log.info("recv - {}", result);
  217. conn.disconnect();
  218. br.close();
  219. }
  220. catch (ConnectException e)
  221. {
  222. log.error("调用HttpUtils.sendSSLPost ConnectException, url=" + url + ",param=" + param, e);
  223. }
  224. catch (SocketTimeoutException e)
  225. {
  226. log.error("调用HttpUtils.sendSSLPost SocketTimeoutException, url=" + url + ",param=" + param, e);
  227. }
  228. catch (IOException e)
  229. {
  230. log.error("调用HttpUtils.sendSSLPost IOException, url=" + url + ",param=" + param, e);
  231. }
  232. catch (Exception e)
  233. {
  234. log.error("调用HttpsUtil.sendSSLPost Exception, url=" + url + ",param=" + param, e);
  235. }
  236. return result.toString();
  237. }
  238. private static class TrustAnyTrustManager implements X509TrustManager
  239. {
  240. @Override
  241. public void checkClientTrusted(X509Certificate[] chain, String authType)
  242. {
  243. }
  244. @Override
  245. public void checkServerTrusted(X509Certificate[] chain, String authType)
  246. {
  247. }
  248. @Override
  249. public X509Certificate[] getAcceptedIssuers()
  250. {
  251. return new X509Certificate[] {};
  252. }
  253. }
  254. private static class TrustAnyHostnameVerifier implements HostnameVerifier
  255. {
  256. @Override
  257. public boolean verify(String hostname, SSLSession session)
  258. {
  259. return true;
  260. }
  261. }
  262. }