From 3cfb3d987566f4baf5563a52dced85c9bd8a5391 Mon Sep 17 00:00:00 2001
From: dd <gitluke@outlook.com>
Date: Fri, 26 Dec 2025 01:47:15 +0800
Subject: [PATCH] 1
---
src/main/java/com/nq/service/impl/PriceServicesImpl.java | 50 ++++++++++++++++++--------------------------------
1 files changed, 18 insertions(+), 32 deletions(-)
diff --git a/src/main/java/com/nq/service/impl/PriceServicesImpl.java b/src/main/java/com/nq/service/impl/PriceServicesImpl.java
index f33d4db..c0a3e1d 100644
--- a/src/main/java/com/nq/service/impl/PriceServicesImpl.java
+++ b/src/main/java/com/nq/service/impl/PriceServicesImpl.java
@@ -109,54 +109,40 @@
}
}
- public String doGet(String pid){
+ 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 {
+ Thread.sleep(3000);
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- // 设置超时时间
- connection.setConnectTimeout(10000);
- connection.setReadTimeout(10000);
+ // 设置请求方法为 POST
connection.setRequestMethod("POST");
+ // 设置请求头
connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
- connection.setDoOutput(true);
+ connection.setDoOutput(true); // 允许向连接输出
- // JSON格式请求体
- String jsonInputString = "{\"pid\":\"" + pid + "\"}";
+ // 构建 POST 数据
+ String postData = "pid=" + pid;
+ // 发送 POST 请求
try (OutputStream os = connection.getOutputStream()) {
- byte[] input = jsonInputString.getBytes("utf-8");
+ byte[] input = postData.getBytes("utf-8");
os.write(input, 0, input.length);
}
- // 检查HTTP响应码
- int responseCode = connection.getResponseCode();
- if (responseCode == 429) {
- // 如果还是429,等待更长时间后重试
- Thread.sleep(3000);
- return doGet(pid); // 递归重试(注意设置最大重试次数)
- }
-
// 读取响应
- 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();
+ BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+ String inputLine;
+ StringBuffer response = new StringBuffer();
+
+ while ((inputLine = in.readLine()) != null) {
+ response.append(inputLine);
}
+ in.close();
+ result = response.toString();
} catch (Exception e) {
e.printStackTrace();
}
--
Gitblit v1.9.3