073eb02fcd3eaa1b1e09e10bb0235d808b675f34.svn-base 9.0 KB

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