Selaa lähdekoodia

合并0514分支

yousongbo 4 päivää sitten
vanhempi
commit
1b78f1c8fa

+ 54 - 54
suishenbang-common/src/main/java/com/dgtly/common/utils/http/HttpUtils.java

@@ -449,75 +449,75 @@ public class HttpUtils
         }
         return result.toString();
     }
+
+    /**
+     * 向指定 URL 发送HTTPS GET方法的请求(最多重试3次)
+     *
+     * @param url 发送请求的 URL
+     * @return 所代表远程资源的响应结果
+     */
     public static String sendSSLGet(String url)
     {
-        StringBuilder result = new StringBuilder();
-        BufferedReader in = null;
-        try
-        {
-            String urlNameString = url;
-            log.info("sendSSLGet - {}", urlNameString);
-            SSLContext sc = SSLContext.getInstance("SSL");
-            sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
-            URL realUrl = new URL(urlNameString);
+        int maxRetry = 3;
+        Exception lastException = null;
 
-            HttpsURLConnection connection = (HttpsURLConnection)realUrl.openConnection();
-            connection.setRequestProperty("accept", "*/*");
-            connection.setRequestProperty("connection", "Keep-Alive");
-            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
-
-            connection.setSSLSocketFactory(sc.getSocketFactory());
-            connection.setHostnameVerifier(new TrustAnyHostnameVerifier());
-
-            connection.connect();
-            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
-            String line;
-            while ((line = in.readLine()) != null)
-            {
-                result.append(line);
-            }
-            log.info("recv - {}", result);
-        }
-        catch(ConnectException e)
+        for (int attempt = 1; attempt <= maxRetry; attempt++)
         {
-            log.error("调用HttpUtils.sendGet ConnectException, url=" + url , e);
+            StringBuilder result = new StringBuilder();
+            BufferedReader in = null;
+            try
+            {
+                log.info("sendSSLGet - {} (第{}次尝试)", url, attempt);
+                SSLContext sc = SSLContext.getInstance("SSL");
+                sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
+                URL realUrl = new URL(url);
 
-            throw new BusinessException("调用HttpUtils.sendGet ConnectException, url=" + url  );
-        }
-        catch (SocketTimeoutException e)
-        {
-            log.error("调用HttpUtils.sendGet SocketTimeoutException, url=" + url , e);
+                HttpsURLConnection connection = (HttpsURLConnection)realUrl.openConnection();
+                connection.setRequestProperty("accept", "*/*");
+                connection.setRequestProperty("connection", "Keep-Alive");
+                connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
 
-            throw new BusinessException("调用HttpUtils.sendGet SocketTimeoutException, url=" + url  );
+                connection.setSSLSocketFactory(sc.getSocketFactory());
+                connection.setHostnameVerifier(new TrustAnyHostnameVerifier());
 
-        }
-        catch (IOException e)
-        {
-            log.error("调用HttpUtils.sendGet IOException, url=" + url, e);
+                connection.setConnectTimeout(10000);   // 连接超时 10s
+                connection.setReadTimeout(30000);      // 读取超时 30s
 
-            throw new BusinessException("调用HttpUtils.sendGet IOException, url=" + url  );
-        }
-        catch (Exception e)
-        {
-            log.error("调用HttpsUtil.sendGet Exception, url=" + url , e);
 
-            throw new BusinessException("调用HttpUtils.sendGet Exception, url=" + url  );
-        }
-        finally
-        {
-            try
-            {
-                if (in != null)
+                connection.connect();
+                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+                String line;
+                while ((line = in.readLine()) != null)
                 {
-                    in.close();
+                    result.append(line);
                 }
+                log.info("sendSSLGet 成功 - url={}, 第{}次尝试", url, attempt);
+                log.info("recv - {}", result);
+                return result.toString();
             }
-            catch (Exception ex)
+            catch (Exception e)
             {
-                log.error("调用in.close Exception, url=" + url , ex);
+                lastException = e;
+                log.warn("sendSSLGet 第{}次尝试失败, url={}, 异常: {}", attempt, url, e.getMessage());
+            }
+            finally
+            {
+                try
+                {
+                    if (in != null)
+                    {
+                        in.close();
+                    }
+                }
+                catch (Exception ex)
+                {
+                    log.error("调用in.close Exception, url=" + url, ex);
+                }
             }
         }
-        return result.toString();
+
+        log.error("sendSSLGet 重试{}次后仍然失败, url={}", maxRetry, url, lastException);
+        throw new BusinessException("调用HttpUtils.sendSSLGet失败,已重试" + maxRetry + "次, url=" + url);
     }