From e084539a1fbdf8335fcaf088969add8350bbd6a3 Mon Sep 17 00:00:00 2001
From: ZQN <364596817@qq.com>
Date: 星期六, 22 六月 2024 11:51:44 +0800
Subject: [PATCH] 云片短信忽略SSL

---
 project-common/src/main/java/com/project/common/utils/http/HttpUtils.java |  129 +++++++++++++++++++++++++-----------------
 1 files changed, 76 insertions(+), 53 deletions(-)

diff --git a/project-common/src/main/java/com/project/common/utils/http/HttpUtils.java b/project-common/src/main/java/com/project/common/utils/http/HttpUtils.java
index 2f6cc4e..ba16889 100644
--- a/project-common/src/main/java/com/project/common/utils/http/HttpUtils.java
+++ b/project-common/src/main/java/com/project/common/utils/http/HttpUtils.java
@@ -24,10 +24,7 @@
 
 import javax.net.ssl.*;
 import java.io.*;
-import java.net.ConnectException;
-import java.net.SocketTimeoutException;
-import java.net.URL;
-import java.net.URLConnection;
+import java.net.*;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.security.cert.X509Certificate;
@@ -205,59 +202,85 @@
         return result.toString();
     }
 
-    public static String sendSSLPost(String url, String param)
+    public static String sendSSLPost(String httpUrl, String param)
     {
-        StringBuilder result = new StringBuilder();
-        String urlNameString = url + "?" + param;
-        try
-        {
-            log.info("sendSSLPost - {}", urlNameString);
-            SSLContext sc = SSLContext.getInstance("SSL");
-            sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
-            URL console = new URL(urlNameString);
-            HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
-            conn.setRequestProperty("accept", "*/*");
-            conn.setRequestProperty("connection", "Keep-Alive");
-            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
-            conn.setRequestProperty("Accept-Charset", "utf-8");
-            conn.setRequestProperty("contentType", "utf-8");
-            conn.setDoOutput(true);
-            conn.setDoInput(true);
-
-            conn.setSSLSocketFactory(sc.getSocketFactory());
-            conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
-            conn.connect();
-            InputStream is = conn.getInputStream();
-            BufferedReader br = new BufferedReader(new InputStreamReader(is));
-            String ret = "";
-            while ((ret = br.readLine()) != null)
-            {
-                if (ret != null && !"".equals(ret.trim()))
-                {
-                    result.append(new String(ret.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
+        HttpURLConnection connection = null;
+        InputStream is = null;
+        OutputStream os = null;
+        BufferedReader br = null;
+        String result = null;
+        try {
+            URL url = new URL(httpUrl);
+            if("https".equalsIgnoreCase(url.getProtocol())){
+                SslUtils.ignoreSsl();
+            }
+            // 閫氳繃杩滅▼url杩炴帴瀵硅薄鎵撳紑杩炴帴
+            connection = (HttpURLConnection) url.openConnection();
+            // 璁剧疆杩炴帴璇锋眰鏂瑰紡
+            connection.setRequestMethod("POST");
+            // 璁剧疆杩炴帴涓绘満鏈嶅姟鍣ㄨ秴鏃舵椂闂达細15000姣
+            connection.setConnectTimeout(15000);
+            // 璁剧疆璇诲彇涓绘満鏈嶅姟鍣ㄨ繑鍥炴暟鎹秴鏃舵椂闂达細60000姣
+            connection.setReadTimeout(60000);
+            // 榛樿鍊间负锛歠alse锛屽綋鍚戣繙绋嬫湇鍔″櫒浼犻�佹暟鎹�/鍐欐暟鎹椂锛岄渶瑕佽缃负true
+            connection.setDoOutput(true);
+            // 榛樿鍊间负锛歵rue锛屽綋鍓嶅悜杩滅▼鏈嶅姟璇诲彇鏁版嵁鏃讹紝璁剧疆涓簍rue锛岃鍙傛暟鍙湁鍙棤
+            connection.setDoInput(true);
+            // 璁剧疆浼犲叆鍙傛暟鐨勬牸寮�:璇锋眰鍙傛暟搴旇鏄� name1=value1&name2=value2 鐨勫舰寮忋��
+            connection.setRequestProperty("Content-Type", "application/json");
+            // 寤虹珛杩炴帴
+            connection.connect();
+            // 閫氳繃杩炴帴瀵硅薄鑾峰彇涓�涓緭鍑烘祦瀵硅薄
+            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(),"UTF-8");
+            writer.write(param);
+            writer.flush();
+            // 閫氳繃杩炴帴瀵硅薄鑾峰彇涓�涓緭鍏ユ祦锛屽悜杩滅▼璇诲彇
+            if (connection.getResponseCode() == 200) {
+                is = connection.getInputStream();
+                // 瀵硅緭鍏ユ祦瀵硅薄杩涜鍖呰:charset鏍规嵁宸ヤ綔椤圭洰缁勭殑瑕佹眰鏉ヨ缃�
+                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
+                StringBuffer sbf = new StringBuffer();
+                String temp = null;
+                // 寰幆閬嶅巻涓�琛屼竴琛岃鍙栨暟鎹�
+                while ((temp = br.readLine()) != null) {
+                    sbf.append(temp);
+                    sbf.append("\r");
+                }
+                result = sbf.toString();
+            }
+        } catch (MalformedURLException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        } finally {
+            // 鍏抽棴璧勬簮
+            if (null != br) {
+                try {
+                    br.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
                 }
             }
-            log.info("recv - {}", result);
-            conn.disconnect();
-            br.close();
+            if (null != os) {
+                try {
+                    os.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            if (null != is) {
+                try {
+                    is.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            // 鏂紑涓庤繙绋嬪湴鍧�url鐨勮繛鎺�
+            connection.disconnect();
         }
-        catch (ConnectException e)
-        {
-            log.error("璋冪敤HttpUtils.sendSSLPost ConnectException, url=" + url + ",param=" + param, e);
-        }
-        catch (SocketTimeoutException e)
-        {
-            log.error("璋冪敤HttpUtils.sendSSLPost SocketTimeoutException, url=" + url + ",param=" + param, e);
-        }
-        catch (IOException e)
-        {
-            log.error("璋冪敤HttpUtils.sendSSLPost IOException, url=" + url + ",param=" + param, e);
-        }
-        catch (Exception e)
-        {
-            log.error("璋冪敤HttpsUtil.sendSSLPost Exception, url=" + url + ",param=" + param, e);
-        }
-        return result.toString();
+        return result;
     }
 
     private static class TrustAnyTrustManager implements X509TrustManager

--
Gitblit v1.9.1