From ac542add19d10cd1a2d5efe36d762cec9c7df5b4 Mon Sep 17 00:00:00 2001
From: liujie <1793218484@qq.com>
Date: 星期四, 04 九月 2025 17:58:35 +0800
Subject: [PATCH] update
---
ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpHelper.java | 40 +++++++++++++++++++++++++++++++++++++++-
1 files changed, 39 insertions(+), 1 deletions(-)
diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpHelper.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpHelper.java
index 589d123..7b8b001 100644
--- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpHelper.java
+++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/http/HttpHelper.java
@@ -7,6 +7,12 @@
import java.nio.charset.StandardCharsets;
import javax.servlet.ServletRequest;
import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -52,4 +58,36 @@
}
return sb.toString();
}
-}
+
+ /**
+ * 执行HTTP GET请求并返回响应内容
+ * @param url 请求的URL
+ * @param headers 请求头数组
+ * @return 响应内容字符串
+ */
+ public static String httpGet(String url, org.apache.http.Header[] headers) {
+ CloseableHttpClient httpClient = HttpClients.createDefault();
+ HttpGet httpGet = new HttpGet(url);
+
+ // 设置请求头
+ if (headers != null) {
+ for (org.apache.http.Header header : headers) {
+ httpGet.setHeader(header);
+ }
+ }
+
+ try {
+ CloseableHttpResponse response = httpClient.execute(httpGet);
+ return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
+ } catch (IOException e) {
+ LOGGER.error("执行HTTP GET请求时发生错误:{}", e.getMessage());
+ return null;
+ } finally {
+ try {
+ httpClient.close();
+ } catch (IOException e) {
+ LOGGER.error("关闭HTTP客户端时发生错误:{}", e.getMessage());
+ }
+ }
+ }
+}
\ No newline at end of file
--
Gitblit v1.7.1