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/resources/application.properties | 7 ++-
src/main/java/com/nq/ws/WebSocketClientBeanConfig.java | 2
src/main/java/com/nq/utils/http/HttpClientRequest.java | 71 ++++++++++++++++-------------------
src/main/resources/application.yml | 8 ++-
4 files changed, 42 insertions(+), 46 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) {
diff --git a/src/main/java/com/nq/ws/WebSocketClientBeanConfig.java b/src/main/java/com/nq/ws/WebSocketClientBeanConfig.java
index 988d552..f53887d 100644
--- a/src/main/java/com/nq/ws/WebSocketClientBeanConfig.java
+++ b/src/main/java/com/nq/ws/WebSocketClientBeanConfig.java
@@ -22,7 +22,7 @@
Map<String, WebSocketClient> retMap = new HashMap<>(2);
try {
- WebsocketRunClient websocketRunClient = new WebsocketRunClient(new URI(PropertiesUtil.getProperty("JS_IN_WS_URL")),EStockType.US);
+ WebsocketRunClient websocketRunClient = new WebsocketRunClient(new URI(PropertiesUtil.getProperty("US_WS_URL")),EStockType.US);
websocketRunClient.connect();
websocketRunClient.setConnectionLostTimeout(0);
new Thread(() -> {
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 3c03982..0207233 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -20,7 +20,8 @@
ftp.server.http.prefix=https://img.durocaspitall.com/
redis1.ip=localhost
-redis1.port=6379
+redis1.port=6380
+#redis1.port=6379
redis1.pwd=
redis1.timeout=10000
redis.max.total=50
@@ -52,9 +53,9 @@
JS_IN_WS_URL = ws://api-in-pro-ws.js-stock.top
JS_IN_KEY = xKChgi47AP1NMwMeYI3c
-US_HTTP_API = http://api-us-v2.js-stock.top
+US_HTTP_API = http://api-us-v2.js-stock.top/
US_WS_URL = ws://api-us-v2-ws.js-stock.top
-US_KEY = 9uG4sxnsLsGG9e1RhJJE
+US_KEY = PEl4Sbj7FLqOz3SVqjyC
HK_HTTP_API = http://test.js-stock.top/
HK_WS_URL = ws://test-ws.js-stock.top
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index fd47f7d..1b38161 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -104,7 +104,8 @@
# 地址
host: localhost
# 端口,默认为6379
- port: 6379
+# port: 6379
+ port: 6380
# 数据库索引
database: 0
# 密码
@@ -124,10 +125,11 @@
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://127.0.0.1:3306/stock?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
+ url: jdbc:mysql://127.0.0.1:6306us?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
+# url: jdbc:mysql://127.0.0.1:6306us?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
# url: jdbc:mysql://127.0.0.1:3306/cgstock?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
- password: 123456
+ password: Err;2[eoGFUriwdgr
druid:
# 初始连接数
initialSize: 5
--
Gitblit v1.9.3