|
@@ -458,66 +458,76 @@ public class HttpUtils
|
|
|
*/
|
|
*/
|
|
|
public static String sendSSLGet(String url)
|
|
public static String sendSSLGet(String url)
|
|
|
{
|
|
{
|
|
|
- int maxRetry = 3;
|
|
|
|
|
- Exception lastException = null;
|
|
|
|
|
-
|
|
|
|
|
- for (int attempt = 1; attempt <= maxRetry; attempt++)
|
|
|
|
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
|
|
+ BufferedReader in = null;
|
|
|
|
|
+ try
|
|
|
{
|
|
{
|
|
|
- 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);
|
|
|
|
|
+
|
|
|
|
|
+ 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.setConnectTimeout(10000); // 连接超时 10s
|
|
|
|
|
+ connection.setReadTimeout(30000); // 读取超时 30s
|
|
|
|
|
+
|
|
|
|
|
+ connection.connect();
|
|
|
|
|
+ in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
|
|
+ String line;
|
|
|
|
|
+ while ((line = in.readLine()) != null)
|
|
|
{
|
|
{
|
|
|
- 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);
|
|
|
|
|
|
|
+ result.append(line);
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info("recv - {}", result);
|
|
|
|
|
+ }
|
|
|
|
|
+ catch(ConnectException e)
|
|
|
|
|
+ {
|
|
|
|
|
+ log.error("调用HttpUtils.sendGet ConnectException, 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 ConnectException, url=" + url );
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (SocketTimeoutException e)
|
|
|
|
|
+ {
|
|
|
|
|
+ log.error("调用HttpUtils.sendGet SocketTimeoutException, url=" + url , e);
|
|
|
|
|
|
|
|
- connection.setSSLSocketFactory(sc.getSocketFactory());
|
|
|
|
|
- connection.setHostnameVerifier(new TrustAnyHostnameVerifier());
|
|
|
|
|
|
|
+ throw new BusinessException("调用HttpUtils.sendGet SocketTimeoutException, url=" + url );
|
|
|
|
|
|
|
|
- connection.setConnectTimeout(10000); // 连接超时 10s
|
|
|
|
|
- connection.setReadTimeout(30000); // 读取超时 30s
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (IOException e)
|
|
|
|
|
+ {
|
|
|
|
|
+ log.error("调用HttpUtils.sendGet IOException, url=" + url, e);
|
|
|
|
|
|
|
|
|
|
+ throw new BusinessException("调用HttpUtils.sendGet IOException, url=" + url );
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception e)
|
|
|
|
|
+ {
|
|
|
|
|
+ log.error("调用HttpsUtil.sendGet Exception, url=" + url , e);
|
|
|
|
|
|
|
|
- connection.connect();
|
|
|
|
|
- in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
|
|
- String line;
|
|
|
|
|
- while ((line = in.readLine()) != null)
|
|
|
|
|
|
|
+ throw new BusinessException("调用HttpUtils.sendGet Exception, url=" + url );
|
|
|
|
|
+ }
|
|
|
|
|
+ finally
|
|
|
|
|
+ {
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ if (in != null)
|
|
|
{
|
|
{
|
|
|
- result.append(line);
|
|
|
|
|
|
|
+ in.close();
|
|
|
}
|
|
}
|
|
|
- log.info("sendSSLGet 成功 - url={}, 第{}次尝试", url, attempt);
|
|
|
|
|
- log.info("recv - {}", result);
|
|
|
|
|
- return result.toString();
|
|
|
|
|
}
|
|
}
|
|
|
- catch (Exception e)
|
|
|
|
|
- {
|
|
|
|
|
- lastException = e;
|
|
|
|
|
- log.warn("sendSSLGet 第{}次尝试失败, url={}, 异常: {}", attempt, url, e.getMessage());
|
|
|
|
|
- }
|
|
|
|
|
- finally
|
|
|
|
|
|
|
+ catch (Exception ex)
|
|
|
{
|
|
{
|
|
|
- try
|
|
|
|
|
- {
|
|
|
|
|
- if (in != null)
|
|
|
|
|
- {
|
|
|
|
|
- in.close();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- catch (Exception ex)
|
|
|
|
|
- {
|
|
|
|
|
- log.error("调用in.close Exception, url=" + url, ex);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ log.error("调用in.close Exception, url=" + url , ex);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- log.error("sendSSLGet 重试{}次后仍然失败, url={}", maxRetry, url, lastException);
|
|
|
|
|
- throw new BusinessException("调用HttpUtils.sendSSLGet失败,已重试" + maxRetry + "次, url=" + url);
|
|
|
|
|
|
|
+ return result.toString();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|