| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 | 
							- package com.zanxiang.sdk.util;
 
- import com.zanxiang.common.utils.BaseHttpSSLSocketFactory;
 
- import javax.net.ssl.*;
 
- import java.io.BufferedReader;
 
- import java.io.IOException;
 
- import java.io.InputStreamReader;
 
- import java.io.OutputStreamWriter;
 
- import java.net.URL;
 
- import java.net.URLConnection;
 
- import java.net.URLEncoder;
 
- import java.security.cert.CertificateException;
 
- import java.security.cert.X509Certificate;
 
- import java.util.Iterator;
 
- import java.util.Map;
 
- /**
 
-  * 获取订单url
 
-  */
 
- public class HttpUtil {
 
-     public static final int DEF_CONN_TIMEOUT = 30000;
 
-     public static final int DEF_READ_TIMEOUT = 30000;
 
-     private final static String DEFAULT_ENCODING = "UTF-8";
 
-     public static String postData(String urlStr, String data) {
 
-         return postData(urlStr, data, null);
 
-     }
 
-     public static String postData(String urlStr, String data, String contentType) {
 
-         BufferedReader reader = null;
 
-         try {
 
-             URLConnection conn = initHttps(urlStr);
 
-             if (contentType != null) {
 
-                 conn.setRequestProperty("content-type", contentType);
 
-             }
 
-             OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), DEFAULT_ENCODING);
 
-             if (data == null) {
 
-                 data = "";
 
-             }
 
-             writer.write(data);
 
-             writer.flush();
 
-             writer.close();
 
-             reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), DEFAULT_ENCODING));
 
-             StringBuilder sb = new StringBuilder();
 
-             String line;
 
-             while ((line = reader.readLine()) != null) {
 
-                 sb.append(line);
 
-                 sb.append("\r\n");
 
-             }
 
-             return sb.toString();
 
-         } catch (Exception e) {
 
-             e.printStackTrace();
 
-         } finally {
 
-             try {
 
-                 if (reader != null) {
 
-                     reader.close();
 
-                 }
 
-             } catch (IOException e) {
 
-             }
 
-         }
 
-         return null;
 
-     }
 
-     public static String postDataUrl(String urlStr, String data, String contentType) {
 
-         BufferedReader reader = null;
 
-         try {
 
-             URLConnection conn = initHttps(urlStr);
 
-             if (contentType != null) {
 
-                 conn.setRequestProperty("content-type", contentType);
 
-             }
 
-             OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), DEFAULT_ENCODING);
 
-             if (data == null) {
 
-                 data = "";
 
-             }
 
-             writer.write(data);
 
-             writer.flush();
 
-             writer.close();
 
-             reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), DEFAULT_ENCODING));
 
-             StringBuilder sb = new StringBuilder();
 
-             String line;
 
-             while ((line = reader.readLine()) != null) {
 
-                 sb.append(line);
 
-                 sb.append("\r\n");
 
-             }
 
-             return conn.getURL().toString();
 
-         } catch (Exception e) {
 
-             e.printStackTrace();
 
-         } finally {
 
-             try {
 
-                 if (reader != null) {
 
-                     reader.close();
 
-                 }
 
-             } catch (IOException e) {
 
-             }
 
-         }
 
-         return null;
 
-     }
 
-     /**
 
-      * 初始化http请求参数
 
-      */
 
-     private static HttpsURLConnection initHttps(String url) throws Exception {
 
-         TrustManager[] tm = {new MyX509TrustManager()};
 
- //        System.setProperty("https.protocols", "TLSv1");
 
-         SSLContext sslContext = SSLContext.getInstance("TLS");
 
-         sslContext.init(null, tm, new java.security.SecureRandom());
 
-         // 从上述SSLContext对象中得到SSLSocketFactory对象
 
-         SSLSocketFactory ssf = sslContext.getSocketFactory();
 
-         URL _url = new URL(url);
 
-         HttpsURLConnection http = (HttpsURLConnection) _url.openConnection();
 
-         // 设置域名校验
 
-         http.setHostnameVerifier(new BaseHttpSSLSocketFactory.TrustAnyHostnameVerifier());
 
-         // 连接超时
 
-         http.setConnectTimeout(DEF_CONN_TIMEOUT);
 
-         // 读取超时 --服务器响应比较慢,增大时间
 
-         http.setReadTimeout(DEF_READ_TIMEOUT);
 
-         http.setUseCaches(false);
 
-         http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
 
-         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");
 
-         http.setSSLSocketFactory(ssf);
 
-         http.setDoOutput(true);
 
-         http.setDoInput(true);
 
-         http.connect();
 
-         return http;
 
-     }
 
-     private static class MyX509TrustManager implements X509TrustManager {
 
-         @Override
 
-         public X509Certificate[] getAcceptedIssuers() {
 
-             return null;
 
-         }
 
-         @Override
 
-         public void checkServerTrusted(X509Certificate[] chain, String authType)
 
-                 throws CertificateException {
 
-         }
 
-         @Override
 
-         public void checkClientTrusted(X509Certificate[] chain, String authType)
 
-                 throws CertificateException {
 
-         }
 
-     }
 
-     public static String postData(String urlStr, Map<String, String> data) {
 
-         return postData(urlStr, httpBuildQuery(data), null);
 
-     }
 
-     public static String httpBuildQuery(Map<String, String> array) {
 
-         String reString = null;
 
-         //遍历数组形成akey=avalue&bkey=bvalue&ckey=cvalue形式的的字符串
 
-         Iterator it = array.entrySet().iterator();
 
-         while (it.hasNext()) {
 
-             Map.Entry<String, String> entry = (Map.Entry) it.next();
 
-             String key = entry.getKey();
 
-             String value = entry.getValue();
 
-             reString += key + "=" + value + "&";
 
-         }
 
-         reString = reString.substring(0, reString.length() - 1);
 
-         //将得到的字符串进行处理得到目标格式的字符串
 
-         try {
 
-             reString = URLEncoder.encode(reString, "utf-8");
 
-         } catch (Exception e) {
 
-             throw new RuntimeException("http_build_query error:{}", e);
 
-         }
 
-         reString = reString.replace("%3D", "=").replace("%26", "&");
 
-         return reString;
 
-     }
 
- }
 
 
  |