HttpUtils.java 8.6 KB

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