HttpUtil.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package com.zanxiang.sdk.util;
  2. import com.zanxiang.common.utils.BaseHttpSSLSocketFactory;
  3. import javax.net.ssl.*;
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStreamWriter;
  8. import java.net.URL;
  9. import java.net.URLConnection;
  10. import java.net.URLEncoder;
  11. import java.security.cert.CertificateException;
  12. import java.security.cert.X509Certificate;
  13. import java.util.Iterator;
  14. import java.util.Map;
  15. /**
  16. * 获取订单url
  17. */
  18. public class HttpUtil {
  19. public static final int DEF_CONN_TIMEOUT = 30000;
  20. public static final int DEF_READ_TIMEOUT = 30000;
  21. private final static String DEFAULT_ENCODING = "UTF-8";
  22. public static String postData(String urlStr, String data) {
  23. return postData(urlStr, data, null);
  24. }
  25. public static String postData(String urlStr, String data, String contentType) {
  26. BufferedReader reader = null;
  27. try {
  28. URLConnection conn = initHttps(urlStr);
  29. if (contentType != null) {
  30. conn.setRequestProperty("content-type", contentType);
  31. }
  32. OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), DEFAULT_ENCODING);
  33. if (data == null) {
  34. data = "";
  35. }
  36. writer.write(data);
  37. writer.flush();
  38. writer.close();
  39. reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), DEFAULT_ENCODING));
  40. StringBuilder sb = new StringBuilder();
  41. String line;
  42. while ((line = reader.readLine()) != null) {
  43. sb.append(line);
  44. sb.append("\r\n");
  45. }
  46. return sb.toString();
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. } finally {
  50. try {
  51. if (reader != null) {
  52. reader.close();
  53. }
  54. } catch (IOException e) {
  55. }
  56. }
  57. return null;
  58. }
  59. public static String postDataUrl(String urlStr, String data, String contentType) {
  60. BufferedReader reader = null;
  61. try {
  62. URLConnection conn = initHttps(urlStr);
  63. if (contentType != null) {
  64. conn.setRequestProperty("content-type", contentType);
  65. }
  66. OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), DEFAULT_ENCODING);
  67. if (data == null) {
  68. data = "";
  69. }
  70. writer.write(data);
  71. writer.flush();
  72. writer.close();
  73. reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), DEFAULT_ENCODING));
  74. StringBuilder sb = new StringBuilder();
  75. String line;
  76. while ((line = reader.readLine()) != null) {
  77. sb.append(line);
  78. sb.append("\r\n");
  79. }
  80. return conn.getURL().toString();
  81. } catch (Exception e) {
  82. e.printStackTrace();
  83. } finally {
  84. try {
  85. if (reader != null) {
  86. reader.close();
  87. }
  88. } catch (IOException e) {
  89. }
  90. }
  91. return null;
  92. }
  93. /**
  94. * 初始化http请求参数
  95. */
  96. private static HttpsURLConnection initHttps(String url) throws Exception {
  97. TrustManager[] tm = {new MyX509TrustManager()};
  98. // System.setProperty("https.protocols", "TLSv1");
  99. SSLContext sslContext = SSLContext.getInstance("TLS");
  100. sslContext.init(null, tm, new java.security.SecureRandom());
  101. // 从上述SSLContext对象中得到SSLSocketFactory对象
  102. SSLSocketFactory ssf = sslContext.getSocketFactory();
  103. URL _url = new URL(url);
  104. HttpsURLConnection http = (HttpsURLConnection) _url.openConnection();
  105. // 设置域名校验
  106. http.setHostnameVerifier(new BaseHttpSSLSocketFactory.TrustAnyHostnameVerifier());
  107. // 连接超时
  108. http.setConnectTimeout(DEF_CONN_TIMEOUT);
  109. // 读取超时 --服务器响应比较慢,增大时间
  110. http.setReadTimeout(DEF_READ_TIMEOUT);
  111. http.setUseCaches(false);
  112. http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  113. http.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36");
  114. http.setSSLSocketFactory(ssf);
  115. http.setDoOutput(true);
  116. http.setDoInput(true);
  117. http.connect();
  118. return http;
  119. }
  120. private static class MyX509TrustManager implements X509TrustManager {
  121. @Override
  122. public X509Certificate[] getAcceptedIssuers() {
  123. return null;
  124. }
  125. @Override
  126. public void checkServerTrusted(X509Certificate[] chain, String authType)
  127. throws CertificateException {
  128. }
  129. @Override
  130. public void checkClientTrusted(X509Certificate[] chain, String authType)
  131. throws CertificateException {
  132. }
  133. }
  134. public static String postData(String urlStr, Map<String, String> data) {
  135. return postData(urlStr, httpBuildQuery(data), null);
  136. }
  137. public static String httpBuildQuery(Map<String, String> array) {
  138. String reString = null;
  139. //遍历数组形成akey=avalue&bkey=bvalue&ckey=cvalue形式的的字符串
  140. Iterator it = array.entrySet().iterator();
  141. while (it.hasNext()) {
  142. Map.Entry<String, String> entry = (Map.Entry) it.next();
  143. String key = entry.getKey();
  144. String value = entry.getValue();
  145. reString += key + "=" + value + "&";
  146. }
  147. reString = reString.substring(0, reString.length() - 1);
  148. //将得到的字符串进行处理得到目标格式的字符串
  149. try {
  150. reString = URLEncoder.encode(reString, "utf-8");
  151. } catch (Exception e) {
  152. throw new RuntimeException("http_build_query error:{}", e);
  153. }
  154. reString = reString.replace("%3D", "=").replace("%26", "&");
  155. return reString;
  156. }
  157. }