From dba8004e599c935e572c20682b5d463f904e72aa Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Mon, 18 Aug 2025 15:55:52 +0800
Subject: [PATCH] 1

---
 src/main/java/com/nq/utils/http/HttpClientRequest.java |   71 ++++++++++++++++-------------------
 1 files changed, 32 insertions(+), 39 deletions(-)

diff --git a/src/main/java/com/nq/utils/http/HttpClientRequest.java b/src/main/java/com/nq/utils/http/HttpClientRequest.java
index b82a37b..5569a38 100644
--- a/src/main/java/com/nq/utils/http/HttpClientRequest.java
+++ b/src/main/java/com/nq/utils/http/HttpClientRequest.java
@@ -17,6 +17,8 @@
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.util.EntityUtils;
+import org.junit.platform.commons.logging.Logger;
+import org.junit.platform.commons.logging.LoggerFactory;
 
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
@@ -25,55 +27,46 @@
 @Slf4j
 public class HttpClientRequest {
 
+    // 可配置的超时参数(单位:毫秒)
+    private static final int CONNECT_TIMEOUT = 35000;
+    private static final int CONNECTION_REQUEST_TIMEOUT = 35000;
+    private static final int SOCKET_TIMEOUT = 60000;
+
     public static String doGet(String url) {
-        CloseableHttpClient httpClient = null;
-        CloseableHttpResponse response = null;
-        String result = "";
+        // 1. 参数校验
+        if (url == null || url.trim().isEmpty()) {
+            throw new IllegalArgumentException("URL cannot be null or empty");
+        }
 
-        try {
-            httpClient = HttpClients.createDefault();
-
+        // 2. 创建HTTP客户端和请求对象
+        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
             HttpGet httpGet = new HttpGet(url);
 
-            httpGet.setHeader("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
-            httpGet.setHeader("Referer","https://quotes.sina.cn/hs/company/quotes/view/sz399001?vt=4&cid=76524&node_id=76524&autocallup=no&isfromsina=yes");
-            //cookie
-            httpGet.setHeader("Cookie", PropertiesUtil.getProperty("cookle"));
-
-
-            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
-
+            // 3. 配置请求参数
+            RequestConfig requestConfig = RequestConfig.custom()
+                    .setConnectTimeout(CONNECT_TIMEOUT)
+                    .setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)
+                    .setSocketTimeout(SOCKET_TIMEOUT)
+                    .build();
             httpGet.setConfig(requestConfig);
 
-            response = httpClient.execute(httpGet);
+            // 4. 执行请求并处理响应
+            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
+                int statusCode = response.getStatusLine().getStatusCode();
+                if (statusCode != 200) {
+                    throw new IOException("HTTP request failed with status code: " + statusCode);
+                }
 
-            HttpEntity entity = response.getEntity();
+                HttpEntity entity = response.getEntity();
+                if (entity == null) {
+                    throw new IOException("Response entity is null");
+                }
 
-            result = EntityUtils.toString(entity);
-        } catch (ClientProtocolException e) {
-            e.printStackTrace();
+                return EntityUtils.toString(entity);
+            }
         } catch (IOException e) {
-            e.printStackTrace();
+            throw new RuntimeException("HTTP request failed", e);
         }
-        finally {
-
-            if (null != response) {
-
-                try {
-                    response.close();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-            }
-            if (null != httpClient) {
-                try {
-                    httpClient.close();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-            }
-        }
-        return result;
     }
 
     public static String doPost(String url, Map<String, Object> paramMap) {

--
Gitblit v1.9.3