From 6b6190b57b2a10b1b21cd8ecfb57bfeb898b1c69 Mon Sep 17 00:00:00 2001
From: dd <gitluke@outlook.com>
Date: Fri, 26 Dec 2025 00:49:53 +0800
Subject: [PATCH] 1
---
src/main/java/com/nq/service/impl/PriceServicesImpl.java | 51 +++++++++++++++++++++++++++++++++------------------
1 files changed, 33 insertions(+), 18 deletions(-)
diff --git a/src/main/java/com/nq/service/impl/PriceServicesImpl.java b/src/main/java/com/nq/service/impl/PriceServicesImpl.java
index 457b6af..f33d4db 100644
--- a/src/main/java/com/nq/service/impl/PriceServicesImpl.java
+++ b/src/main/java/com/nq/service/impl/PriceServicesImpl.java
@@ -110,38 +110,53 @@
}
public String doGet(String pid){
- // 从配置中获取 API URL,并拼接 key
String apiUrl = PropertiesUtil.getProperty("JS_IN_HTTP_URL") + "stock?key=" + PropertiesUtil.getProperty("JS_IN_KEY");
String result = null;
+
+ try {
+ // 添加请求间隔,避免频率过高
+ Thread.sleep(1000); // 等待1秒再请求
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+
try {
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- // 设置请求方法为 POST
+ // 设置超时时间
+ connection.setConnectTimeout(10000);
+ connection.setReadTimeout(10000);
connection.setRequestMethod("POST");
- // 设置请求头
- connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- connection.setDoOutput(true); // 允许向连接输出
+ connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
+ connection.setDoOutput(true);
- // 构建 POST 数据
- String postData = "pid=" + pid;
+ // JSON格式请求体
+ String jsonInputString = "{\"pid\":\"" + pid + "\"}";
- // 发送 POST 请求
try (OutputStream os = connection.getOutputStream()) {
- byte[] input = postData.getBytes("utf-8");
+ byte[] input = jsonInputString.getBytes("utf-8");
os.write(input, 0, input.length);
}
- // 读取响应
- BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- String inputLine;
- StringBuffer response = new StringBuffer();
-
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
+ // 检查HTTP响应码
+ int responseCode = connection.getResponseCode();
+ if (responseCode == 429) {
+ // 如果还是429,等待更长时间后重试
+ Thread.sleep(3000);
+ return doGet(pid); // 递归重试(注意设置最大重试次数)
}
- in.close();
- result = response.toString();
+
+ // 读取响应
+ try (BufferedReader in = new BufferedReader(
+ new InputStreamReader(connection.getInputStream()))) {
+ String inputLine;
+ StringBuilder response = new StringBuilder();
+ while ((inputLine = in.readLine()) != null) {
+ response.append(inputLine);
+ }
+ result = response.toString();
+ }
} catch (Exception e) {
e.printStackTrace();
}
--
Gitblit v1.9.3