From c1147646b9ef1d713a202d7ab8cf3ea8d677f142 Mon Sep 17 00:00:00 2001 From: wlzboy <66905212@qq.com> Date: 星期六, 27 九月 2025 21:56:54 +0800 Subject: [PATCH] fix:优化评价 --- ruoyi-common/src/main/java/com/ruoyi/common/utils/HttpUtil.java | 74 ++++++++++++++++++++++++++++++++++++- 1 files changed, 72 insertions(+), 2 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/HttpUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/HttpUtil.java index 9afdb90..336143d 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/HttpUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/HttpUtil.java @@ -19,8 +19,8 @@ */ public class HttpUtil { - private static final int CONNECT_TIMEOUT = 5000; - private static final int READ_TIMEOUT = 5000; + private static final int CONNECT_TIMEOUT = 60000; + private static final int READ_TIMEOUT = 60000; static { try { @@ -116,4 +116,74 @@ return response.toString(); } + + /** + * 鍙戦�丟ET璇锋眰 + * @param url 璇锋眰URL + * @param params 璇锋眰鍙傛暟 + * @return 鍝嶅簲鍐呭 + */ + public static String get(String url, Map<String, String> params) { + StringBuilder response = new StringBuilder(); + HttpURLConnection conn = null; + try { + // 鏋勫缓甯﹀弬鏁扮殑URL + StringBuilder urlBuilder = new StringBuilder(url); + if (params != null && !params.isEmpty()) { + urlBuilder.append("?"); + for (Map.Entry<String, String> entry : params.entrySet()) { + urlBuilder.append(entry.getKey()) + .append("=") + .append(entry.getValue()) + .append("&"); + } + urlBuilder.deleteCharAt(urlBuilder.length() - 1); // 鍒犻櫎鏈�鍚庝竴涓�& + } + + // 鍒涘缓杩炴帴 + URL requestUrl = new URL(urlBuilder.toString()); + boolean isHttps = url.toLowerCase().startsWith("https"); + + // 鏍规嵁鍗忚绫诲瀷鍒涘缓杩炴帴 + if (isHttps) { + conn = (HttpsURLConnection) requestUrl.openConnection(); + } else { + conn = (HttpURLConnection) requestUrl.openConnection(); + } + + // 璁剧疆璇锋眰灞炴�� + conn.setRequestMethod("GET"); + conn.setDoInput(true); + conn.setUseCaches(false); + conn.setRequestProperty("Accept", "application/json"); + + // 璁剧疆瓒呮椂鏃堕棿 + conn.setConnectTimeout(CONNECT_TIMEOUT); + conn.setReadTimeout(READ_TIMEOUT); + + // 鑾峰彇鍝嶅簲鐮� + int responseCode = conn.getResponseCode(); + if (responseCode != HttpURLConnection.HTTP_OK) { + throw new RuntimeException("HTTP璇锋眰澶辫触锛屽搷搴旂爜: " + responseCode); + } + + // 璇诲彇鍝嶅簲 + try (BufferedReader reader = new BufferedReader( + new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) { + String line; + while ((line = reader.readLine()) != null) { + response.append(line); + } + } + + } catch (Exception e) { + throw new RuntimeException("HTTP璇锋眰澶辫触: " + e.getMessage(), e); + } finally { + if (conn != null) { + conn.disconnect(); + } + } + + return response.toString(); + } } \ No newline at end of file -- Gitblit v1.9.1