WebRequest.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. *
  3. */
  4. package com.cnd3b.utility;
  5. import javax.net.ssl.HostnameVerifier;
  6. import javax.net.ssl.HttpsURLConnection;
  7. import javax.net.ssl.SSLSession;
  8. import java.io.*;
  9. import java.net.HttpURLConnection;
  10. import java.net.URL;
  11. import java.nio.charset.StandardCharsets;
  12. /**
  13. * @author SJW
  14. *
  15. */
  16. public class WebRequest {
  17. public String doPost(String content, String url) {
  18. PrintWriter out = null;
  19. BufferedReader in = null;
  20. StringBuilder result = new StringBuilder();
  21. HttpURLConnection conn = null;
  22. try {
  23. URL realUrl = new URL(url);
  24. // 打开和URL之间的连接
  25. conn = (HttpURLConnection) realUrl.openConnection();
  26. conn.setRequestProperty("accept", "*/*");
  27. conn.setRequestProperty("connection", "Keep-Alive");
  28. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  29. conn.setConnectTimeout(8000);
  30. conn.setDoOutput(true);
  31. conn.setDoInput(true);
  32. out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8));
  33. out.print(content);
  34. out.flush();
  35. in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
  36. String line;
  37. while ((line = in.readLine()) != null) {
  38. result.append(line);
  39. }
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. System.err.println("[POST请求]向地址:" + url + " 发送数据:发生错误!");
  43. } finally {// 使用finally块来关闭输出流、输入流
  44. if (out != null) {
  45. out.close();
  46. out = null;
  47. }
  48. if (in != null) {
  49. try {
  50. in.close();
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54. in = null;
  55. }
  56. if (conn != null) {
  57. conn.disconnect();
  58. conn = null;
  59. }
  60. }
  61. return result.toString();
  62. }
  63. public String doPostKuJiaLe(String content, String url) {
  64. PrintWriter out = null;
  65. BufferedReader in = null;
  66. StringBuilder result = new StringBuilder();
  67. HttpURLConnection conn = null;
  68. try {
  69. URL realUrl = new URL(url);
  70. // 打开和URL之间的连接
  71. conn = (HttpURLConnection) realUrl.openConnection();
  72. conn.setRequestProperty("accept", "*/*");
  73. conn.setRequestProperty("connection", "Keep-Alive");
  74. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  75. conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
  76. conn.setConnectTimeout(8000);
  77. conn.setDoOutput(true);
  78. conn.setDoInput(true);
  79. out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8));
  80. out.print(content);
  81. out.flush();
  82. in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
  83. String line;
  84. while ((line = in.readLine()) != null) {
  85. result.append(line);
  86. }
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. System.err.println("[POST请求]向地址:" + url + " 发送数据:发生错误!");
  90. } finally {// 使用finally块来关闭输出流、输入流
  91. if (out != null) {
  92. out.close();
  93. out = null;
  94. }
  95. if (in != null) {
  96. try {
  97. in.close();
  98. } catch (IOException e) {
  99. e.printStackTrace();
  100. }
  101. in = null;
  102. }
  103. if (conn != null) {
  104. conn.disconnect();
  105. conn = null;
  106. }
  107. }
  108. return result.toString();
  109. }
  110. public void doGet_simple(String url) {
  111. HttpsURLConnection conn = null;
  112. try {
  113. URL realUrl = new URL(url);
  114. trustAllHttpsCertificates();
  115. HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
  116. @Override
  117. public boolean verify(String hostname, SSLSession session) {
  118. // TODO Auto-generated method stub
  119. System.out.println("Warning: URL Host: " + hostname + " vs. "
  120. + session.getPeerHost());
  121. return true;
  122. }
  123. });
  124. conn = (HttpsURLConnection) realUrl.openConnection();
  125. conn.setConnectTimeout(8000);
  126. conn.getResponseCode();
  127. } catch (Exception e) {
  128. e.printStackTrace();
  129. System.err.println("[POST请求]向地址:" + url + " 发送数据:发生错误!");
  130. } finally {// 使用finally块来关闭输出流、输入流
  131. if (conn != null) {
  132. conn.disconnect();
  133. }
  134. }
  135. }
  136. public String doGet(String url) {
  137. PrintWriter out = null;
  138. BufferedReader in = null;
  139. StringBuilder result = new StringBuilder();
  140. HttpsURLConnection conn = null;
  141. try {
  142. URL realUrl = new URL(url);
  143. trustAllHttpsCertificates();
  144. HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
  145. @Override
  146. public boolean verify(String hostname, SSLSession session) {
  147. // TODO Auto-generated method stub
  148. System.out.println("Warning: URL Host: " + hostname + " vs. "
  149. + session.getPeerHost());
  150. return true;
  151. }
  152. });
  153. // 打开和URL之间的连接
  154. conn = (HttpsURLConnection) realUrl.openConnection();
  155. conn.setRequestProperty("accept", "*/*");
  156. conn.setRequestProperty("connection", "Keep-Alive");
  157. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  158. conn.setConnectTimeout(8000);
  159. in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
  160. String line;
  161. while ((line = in.readLine()) != null) {
  162. result.append(line);
  163. }
  164. } catch (Exception e) {
  165. e.printStackTrace();
  166. System.err.println("[POST请求]向地址:" + url + " 发送数据:发生错误!");
  167. } finally {// 使用finally块来关闭输出流、输入流
  168. if (out != null) {
  169. out.close();
  170. out = null;
  171. }
  172. if (in != null) {
  173. try {
  174. in.close();
  175. } catch (IOException e) {
  176. e.printStackTrace();
  177. }
  178. in = null;
  179. }
  180. if (conn != null) {
  181. conn.disconnect();
  182. conn = null;
  183. }
  184. }
  185. return result.toString();
  186. }
  187. private static void trustAllHttpsCertificates() throws Exception {
  188. javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
  189. javax.net.ssl.TrustManager tm = new miTM();
  190. trustAllCerts[0] = tm;
  191. javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
  192. sc.init(null, trustAllCerts, null);
  193. javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
  194. }
  195. static class miTM implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {
  196. public java.security.cert.X509Certificate[] getAcceptedIssuers() {
  197. return null;
  198. }
  199. public boolean isServerTrusted(java.security.cert.X509Certificate[] certs) {
  200. return true;
  201. }
  202. public boolean isClientTrusted(java.security.cert.X509Certificate[] certs) {
  203. return true;
  204. }
  205. public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
  206. throws java.security.cert.CertificateException {
  207. }
  208. public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
  209. throws java.security.cert.CertificateException {
  210. }
  211. }
  212. }