From 1cf91e355038837f30f2d727507b2229263d7de7 Mon Sep 17 00:00:00 2001
From: liyong <18434998025@163.com>
Date: 星期二, 27 一月 2026 17:49:03 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/dev_New' into dev_New
---
src/main/java/com/ruoyi/common/utils/http/HttpUtils.java | 128 +++++++++++++++++++++++++++++++++++++++---
1 files changed, 118 insertions(+), 10 deletions(-)
diff --git a/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java b/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java
index f57baf0..0a16b62 100644
--- a/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java
+++ b/src/main/java/com/ruoyi/common/utils/http/HttpUtils.java
@@ -9,6 +9,7 @@
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
+import java.nio.charset.StandardCharsets;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
@@ -20,6 +21,7 @@
import org.slf4j.LoggerFactory;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
+import org.springframework.http.MediaType;
/**
* 閫氱敤http鍙戦�佹柟娉�
@@ -73,7 +75,7 @@
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
- connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
+ connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
connection.connect();
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
String line;
@@ -125,26 +127,38 @@
*/
public static String sendPost(String url, String param)
{
+ return sendPost(url, param, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
+ }
+
+ /**
+ * 鍚戞寚瀹� URL 鍙戦�丳OST鏂规硶鐨勮姹�
+ *
+ * @param url 鍙戦�佽姹傜殑 URL
+ * @param param 璇锋眰鍙傛暟
+ * @param contentType 鍐呭绫诲瀷
+ * @return 鎵�浠h〃杩滅▼璧勬簮鐨勫搷搴旂粨鏋�
+ */
+ public static String sendPost(String url, String param, String contentType)
+ {
PrintWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
try
{
- String urlNameString = url;
- log.info("sendPost - {}", urlNameString);
- URL realUrl = new URL(urlNameString);
+ log.info("sendPost - {}", url);
+ URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
- conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
+ conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
conn.setRequestProperty("Accept-Charset", "utf-8");
- conn.setRequestProperty("contentType", "utf-8");
+ conn.setRequestProperty("Content-Type", contentType);
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.flush();
- in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
+ in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
String line;
while ((line = in.readLine()) != null)
{
@@ -191,6 +205,11 @@
public static String sendSSLPost(String url, String param)
{
+ return sendSSLPost(url, param, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
+ }
+
+ public static String sendSSLPost(String url, String param, String contentType)
+ {
StringBuilder result = new StringBuilder();
String urlNameString = url + "?" + param;
try
@@ -202,9 +221,9 @@
HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
- conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
+ conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
conn.setRequestProperty("Accept-Charset", "utf-8");
- conn.setRequestProperty("contentType", "utf-8");
+ conn.setRequestProperty("Content-Type", contentType);
conn.setDoOutput(true);
conn.setDoInput(true);
@@ -218,7 +237,7 @@
{
if (ret != null && !"".equals(ret.trim()))
{
- result.append(new String(ret.getBytes("ISO-8859-1"), "utf-8"));
+ result.append(new String(ret.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
}
}
log.info("recv - {}", result);
@@ -271,4 +290,93 @@
return true;
}
}
+
+ /**
+ * 鍚戞寚瀹歎RL鍙戦�丣SON鏍煎紡鐨凱OST璇锋眰锛堟帹鑽愰�氱敤鏂规硶锛岄粯璁TF-8缂栫爜锛�
+ * @param url 鍙戦�佽姹傜殑URL
+ * @param jsonParam JSON鏍煎紡鐨勮姹傚弬鏁帮紙鐩存帴浼犲叆JSON瀛楃涓诧級
+ * @return 杩滅▼璧勬簮鐨勫搷搴旂粨鏋�
+ */
+ public static String sendPostJson(String url, String jsonParam) {
+ // 閲嶈浇璋冪敤锛岄粯璁や娇鐢║TF-8缂栫爜锛孋ontent-Type鍥哄畾涓篴pplication/json
+ return sendPostJson(url, jsonParam, StandardCharsets.UTF_8.name(),null);
+ }
+
+ /**
+ * 鍚戞寚瀹歎RL鍙戦�丣SON鏍煎紡鐨凱OST璇锋眰锛堣嚜瀹氫箟缂栫爜锛岀伒娲婚�傞厤鐗规畩鍦烘櫙锛�
+ * @param url 鍙戦�佽姹傜殑URL
+ * @param jsonParam JSON鏍煎紡鐨勮姹傚弬鏁帮紙鐩存帴浼犲叆JSON瀛楃涓诧級
+ * @param headerValue 璇锋眰澶寸殑鍊�
+ * @return 杩滅▼璧勬簮鐨勫搷搴旂粨鏋�
+ */
+ public static String sendPostJson(String url, String jsonParam, String headerValue) {
+ // 閲嶈浇璋冪敤锛岄粯璁や娇鐢║TF-8缂栫爜锛孋ontent-Type鍥哄畾涓篴pplication/json
+ return sendPostJson(url, jsonParam, StandardCharsets.UTF_8.name(),headerValue);
+ }
+
+ /**
+ * 鍚戞寚瀹歎RL鍙戦�丣SON鏍煎紡鐨凱OST璇锋眰锛堣嚜瀹氫箟缂栫爜锛岀伒娲婚�傞厤鐗规畩鍦烘櫙锛�
+ * @param url 鍙戦�佽姹傜殑URL
+ * @param jsonParam JSON鏍煎紡鐨勮姹傚弬鏁帮紙鐩存帴浼犲叆JSON瀛楃涓诧級
+ * @param charset 缂栫爜绫诲瀷锛堝UTF-8銆丟BK绛夛紝寤鸿浣跨敤StandardCharsets甯搁噺锛�
+ * @return 杩滅▼璧勬簮鐨勫搷搴旂粨鏋�
+ */
+ public static String sendPostJson(String url, String jsonParam, String charset,String headerValue) {
+ PrintWriter out = null;
+ BufferedReader in = null;
+ StringBuilder result = new StringBuilder();
+ try {
+ log.info("sendPostJson - {}", url);
+ URL realUrl = new URL(url);
+ URLConnection conn = realUrl.openConnection();
+
+ // 1. 璁剧疆璇锋眰澶达細鍥哄畾Content-Type涓篴pplication/json锛岄�傞厤JSON浼犲弬瑙勮寖
+ conn.setRequestProperty("accept", "*/*");
+ conn.setRequestProperty("connection", "Keep-Alive");
+ conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
+ conn.setRequestProperty("Content-Type", MediaType.APPLICATION_JSON_VALUE); // 鏍稿績锛欽SON涓撳睘Content-Type
+ conn.setRequestProperty("Accept-Charset", charset);
+ // 1.1 璁剧疆璇锋眰澶达細鍙�夛紝鏍规嵁瀹為檯闇�瑕佽缃�
+ if(StringUtils.isNotEmpty(headerValue)){
+ conn.setRequestProperty("Authorization", headerValue);
+ }
+ // 2. 寮�鍚緭鍏ヨ緭鍑烘祦锛圥OST璇锋眰蹇呴』锛�
+ conn.setDoOutput(true);
+ conn.setDoInput(true);
+
+ // 3. 鍐欏叆JSON鍙傛暟锛堜娇鐢ㄦ寚瀹氱紪鐮侊紝閬垮厤涓枃涔辩爜锛�
+ out = new PrintWriter(conn.getOutputStream(), true);
+ out.print(jsonParam);
+ out.flush();
+
+ // 4. 璇诲彇鍝嶅簲缁撴灉锛堜笌璇锋眰缂栫爜涓�鑷达紝淇濊瘉瑙f瀽姝g‘锛�
+ in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
+ String line;
+ while ((line = in.readLine()) != null) {
+ result.append(line);
+ }
+ log.info("recv - {}", result);
+ } catch (ConnectException e) {
+ log.error("璋冪敤HttpUtils.sendPostJson ConnectException, url=" + url + ",jsonParam=" + jsonParam, e);
+ } catch (SocketTimeoutException e) {
+ log.error("璋冪敤HttpUtils.sendPostJson SocketTimeoutException, url=" + url + ",jsonParam=" + jsonParam, e);
+ } catch (IOException e) {
+ log.error("璋冪敤HttpUtils.sendPostJson IOException, url=" + url + ",jsonParam=" + jsonParam, e);
+ } catch (Exception e) {
+ log.error("璋冪敤HttpUtils.sendPostJson Exception, url=" + url + ",jsonParam=" + jsonParam, e);
+ } finally {
+ // 5. 鍏抽棴娴佽祫婧愶紙閬垮厤鍐呭瓨娉勬紡锛屼笌鍘熶唬鐮乫inally閫昏緫淇濇寔涓�鑷达級
+ try {
+ if (out != null) {
+ out.close();
+ }
+ if (in != null) {
+ in.close();
+ }
+ } catch (IOException ex) {
+ log.error("璋冪敤娴佸叧闂紓甯� sendPostJson, url=" + url + ",jsonParam=" + jsonParam, ex);
+ }
+ }
+ return result.toString();
+ }
}
\ No newline at end of file
--
Gitblit v1.9.3