From c77c1064dc93bc7d4f0a402dd0285082bb354450 Mon Sep 17 00:00:00 2001
From: liusheng <337615773@qq.com>
Date: 星期三, 10 九月 2025 18:25:02 +0800
Subject: [PATCH] HTTP接口修改
---
ruoyi-common/src/main/java/com/ruoyi/common/utils/HttpUtil.java | 163 ++++++++++++++++++++++++++++++++++++++++++++----------
1 files changed, 132 insertions(+), 31 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 42abce1..f0713bc 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
@@ -20,6 +20,9 @@
import org.springframework.web.context.request.ServletRequestAttributes;
import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
@@ -284,50 +287,148 @@
}
}
- public static String postFormRequest(String url, Map<String, String> params) throws HttpRequestException {
- Assert.hasLength(url, "璇锋眰url涓嶈兘涓虹┖瀛楃涓层��");
- Assert.notNull(params, "璇锋眰params涓嶈兘涓虹┖銆�");
-
- PostMethod httpMethod = new PostMethod(url);
-
- httpMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
+ public static String postFormRequest(String baseUrl, Map<String, String> params, Map<String, String> headers, String body) {
+ HttpURLConnection connection = null;
+ BufferedReader reader = null;
try {
- // 鍙戦�佽姹傚弬鏁�
- StringBuilder param = new StringBuilder();
- for (Map.Entry<String, String> entry : params.entrySet()) {
- if (param.length() > 0) {
- param.append("&");
+ // 鏋勫缓瀹屾暣鐨刄RL锛堝寘鍚煡璇㈠弬鏁帮級
+ String fullUrl = buildUrlWithParams(baseUrl, params);
+ URL requestUrl = new URL(fullUrl);
+ connection = (HttpURLConnection) requestUrl.openConnection();
+
+ // 璁剧疆璇锋眰鏂规硶
+ connection.setRequestMethod("POST");
+ connection.setDoOutput(true);
+ connection.setDoInput(true);
+ connection.setUseCaches(false);
+
+ // 璁剧疆璇锋眰澶�
+ if (headers != null) {
+ for (Map.Entry<String, String> entry : headers.entrySet()) {
+ connection.setRequestProperty(entry.getKey(), entry.getValue());
}
- param.append(entry.getKey());
- param.append("=");
- param.append(entry.getValue());
}
- RequestEntity entity = new StringRequestEntity(param.toString(), "application/json", "utf-8");
- httpMethod.setRequestEntity(entity);
- // 鎵ц璇锋眰骞舵帴鏀跺搷搴旂爜
- int resultCode = httpClient.executeMethod(httpMethod);
+ // 璁剧疆榛樿鐨凜ontent-Type
+ if (!connection.getRequestProperties().containsKey("Content-Type")) {
+ connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
+ }
- String respJson = httpMethod.getResponseBodyAsString();
- if (resultCode == OK) {
- return respJson;
+ // 濡傛灉鏈夎姹備綋锛屽啓鍏ユ暟鎹�
+ if (body != null && !body.isEmpty()) {
+ try (OutputStream os = connection.getOutputStream()) {
+ byte[] input = body.getBytes(StandardCharsets.UTF_8);
+ os.write(input, 0, input.length);
+ }
+ }
+
+ // 鑾峰彇鍝嶅簲鐮�
+ int responseCode = connection.getResponseCode();
+
+ // 璇诲彇鍝嶅簲
+ StringBuilder response = new StringBuilder();
+ if (responseCode == HttpURLConnection.HTTP_OK) {
+ reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
} else {
- throw new HttpRequestException(resultCode, respJson);
+ reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
}
- } catch (UnsupportedEncodingException e) {
- throw new HttpRequestException(ENCODING_ERROR_CODE, e);
- } catch (HttpException e) {
- throw new HttpRequestException(HTTP_ERROR_CODE, e);
- } catch (IOException e) {
- throw new HttpRequestException(IO_ERROR_CODE, e);
+
+ String line;
+ while ((line = reader.readLine()) != null) {
+ response.append(line);
+ }
+
+ return response.toString();
+
+ } catch (Exception e) {
+ throw new RuntimeException("POST璇锋眰澶辫触: " + e.getMessage(), e);
} finally {
- if (httpMethod != null) {
- httpMethod.releaseConnection();
+ // 鍏抽棴杩炴帴
+ if (reader != null) {
+ try {
+ reader.close();
+ } catch (Exception e) { /* ignore */ }
+ }
+ if (connection != null) {
+ connection.disconnect();
}
}
}
+ private static String buildUrlWithParams(String baseUrl, Map<String, String> params) {
+ if (params == null || params.isEmpty()) {
+ return baseUrl;
+ }
+
+ StringBuilder urlBuilder = new StringBuilder(baseUrl);
+ boolean firstParam = true;
+
+ for (Map.Entry<String, String> entry : params.entrySet()) {
+ if (firstParam) {
+ urlBuilder.append("?");
+ firstParam = false;
+ } else {
+ urlBuilder.append("&");
+ }
+
+ try {
+ urlBuilder.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8.toString())).append("=").append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.toString()));
+ } catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
+ }
+ }
+
+ return urlBuilder.toString();
+ }
+// public static String postFormRequest(String url, Map<String, String> params,Map<String, String> headers) throws HttpRequestException {
+// Assert.hasLength(url, "璇锋眰url涓嶈兘涓虹┖瀛楃涓层��");
+// Assert.notNull(params, "璇锋眰params涓嶈兘涓虹┖銆�");
+//
+// PostMethod httpMethod = new PostMethod(url);
+//
+// httpMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
+//
+// if(!ObjectUtils.isEmpty(headers)) {
+// httpMethod.addRequestHeader(headers.get(), "application/json");
+// }
+//
+// try {
+// // 鍙戦�佽姹傚弬鏁�
+// StringBuilder param = new StringBuilder();
+// for (Map.Entry<String, String> entry : params.entrySet()) {
+// if (param.length() > 0) {
+// param.append("&");
+// }
+// param.append(entry.getKey());
+// param.append("=");
+// param.append(entry.getValue());
+// }
+//
+// RequestEntity entity = new StringRequestEntity(param.toString(), "application/json", "utf-8");
+// httpMethod.setRequestEntity(entity);
+// // 鎵ц璇锋眰骞舵帴鏀跺搷搴旂爜
+// int resultCode = httpClient.executeMethod(httpMethod);
+//
+// String respJson = httpMethod.getResponseBodyAsString();
+// if (resultCode == OK) {
+// return respJson;
+// } else {
+// throw new HttpRequestException(resultCode, respJson);
+// }
+// } catch (UnsupportedEncodingException e) {
+// throw new HttpRequestException(ENCODING_ERROR_CODE, e);
+// } catch (HttpException e) {
+// throw new HttpRequestException(HTTP_ERROR_CODE, e);
+// } catch (IOException e) {
+// throw new HttpRequestException(IO_ERROR_CODE, e);
+// } finally {
+// if (httpMethod != null) {
+// httpMethod.releaseConnection();
+// }
+// }
+// }
+
private static Map<String, String> getRespHeaders(HttpMethodBase httpMethod) {
//寰楀埌鍝嶅簲澶�
--
Gitblit v1.9.3