From 91f82652d3a19efa3da0be659aa8954bbde2c712 Mon Sep 17 00:00:00 2001
From: zyy <zyy@email.com>
Date: Tue, 28 Oct 2025 14:03:59 +0800
Subject: [PATCH] 1
---
src/main/java/com/nq/utils/http/HttpRequest.java | 32 ++++++++++++--------------------
1 files changed, 12 insertions(+), 20 deletions(-)
diff --git a/src/main/java/com/nq/utils/http/HttpRequest.java b/src/main/java/com/nq/utils/http/HttpRequest.java
index 409d7b0..a41fafa 100644
--- a/src/main/java/com/nq/utils/http/HttpRequest.java
+++ b/src/main/java/com/nq/utils/http/HttpRequest.java
@@ -68,48 +68,40 @@
/*
*抓數據專用,
- *signature:授權碼
- * timestamp:時間戳
* url
*/
public static String doGrabGet(String url) throws Exception {
- // Request URL: http://gateway.jinyi999.cn/rjhy-news/api/1/tcy/news/hotlist?columnCodes=cjyw&appCode=tcy&showPermission=0&limit=20&hasContent=0&pageNo=1
URL localURL = new URL(url);
URLConnection connection = localURL.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
- InputStream inputStream = null;
- InputStreamReader inputStreamReader = null;
- BufferedReader reader = null;
+ httpURLConnection.setConnectTimeout(12000); // 设置连接超时时间
+ httpURLConnection.setReadTimeout(12000); // 设置读取超时时间
StringBuffer resultBuffer = new StringBuffer();
- String tempLine = null;
if (httpURLConnection.getResponseCode() >= 300) {
+ throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
}
- try {
- inputStream = httpURLConnection.getInputStream();
- inputStreamReader = new InputStreamReader(inputStream);
- reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
+ try (InputStream inputStream = httpURLConnection.getInputStream();
+ InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
+ BufferedReader reader = new BufferedReader(inputStreamReader)) {
+
+ String tempLine;
while ((tempLine = reader.readLine()) != null) {
resultBuffer.append(tempLine);
}
+
} finally {
- if (reader != null) {
- reader.close();
- }
- if (inputStreamReader != null) {
- inputStreamReader.close();
- }
- if (inputStream != null) {
- inputStream.close();
- }
+ httpURLConnection.disconnect(); // 断开连接,释放资源
}
+
return resultBuffer.toString();
}
+
public static String doPost(String url, Map<String, String> params) throws Exception {
HttpPost httpPost = new HttpPost(url);
--
Gitblit v1.9.3