From 9dd748e0352f31b6255be2860c177db0d20ac40e Mon Sep 17 00:00:00 2001
From: liusheng <337615773@qq.com>
Date: 星期二, 21 十月 2025 14:41:35 +0800
Subject: [PATCH] Merge branch 'master' into 0929-master
---
ruoyi-common/src/main/java/com/ruoyi/common/utils/HttpUtil.java | 198 +++++++++++++++++++++++++++++++++++++------------
1 files changed, 148 insertions(+), 50 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 6af7a27..859672d 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
@@ -15,32 +15,20 @@
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpClientParams;
-import org.apache.http.NameValuePair;
-import org.apache.http.client.ClientProtocolException;
-import org.apache.http.client.config.RequestConfig;
-import org.apache.http.client.entity.UrlEncodedFormEntity;
-import org.apache.http.client.methods.CloseableHttpResponse;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.client.utils.URIBuilder;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClients;
-import org.apache.http.message.BasicNameValuePair;
-import org.apache.http.util.EntityUtils;
import org.springframework.util.Assert;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
-import javax.net.ssl.SSLContext;
-import javax.net.ssl.SSLSocketFactory;
-import javax.net.ssl.TrustManager;
-import javax.net.ssl.X509TrustManager;
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.security.NoSuchAlgorithmException;
-import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
-import java.util.*;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
@Slf4j
public class HttpUtil {
@@ -118,11 +106,23 @@
httpMethod.setRequestEntity(entity);
int resultCode = httpClient.executeMethod(httpMethod);
+ // 鑾峰彇鍝嶅簲澶寸殑瀛楃缂栫爜
+ String contentType = httpMethod.getResponseHeader("Content-Type") == null ?
+ null : httpMethod.getResponseHeader("Content-Type").getValue();
+ String charset = "UTF-8"; // 榛樿浣跨敤UTF-8
+
+ if (contentType != null && contentType.contains("charset=")) {
+ String[] parts = contentType.split("charset=");
+ if (parts.length > 1) {
+ charset = parts[1].split(";")[0].trim();
+ }
+ }
+
InputStream inputStream = httpMethod.getResponseBodyAsStream();
if (inputStream == null) {
throw new HttpRequestException(RESPONSE_NULL_ERROR_CODE, "鍝嶅簲涓簄ull");
}
- BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
+ BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,charset));
StringBuffer stringBuffer = new StringBuffer();
String str = "";
while ((str = reader.readLine()) != null) {
@@ -299,50 +299,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