1
dd
2025-12-26 6b6190b57b2a10b1b21cd8ecfb57bfeb898b1c69
1
1 files modified
51 ■■■■■ changed files
src/main/java/com/nq/service/impl/PriceServicesImpl.java 51 ●●●●● patch | view | raw | blame | history
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();
        }