From 19dde21187ef9edfdb38f60fbe764bddf3d2466d Mon Sep 17 00:00:00 2001
From: sinake <sinake1@qq.com>
Date: 星期六, 13 九月 2025 14:39:29 +0800
Subject: [PATCH] 登录用户没取科室病区

---
 ruoyi-common/src/main/java/com/ruoyi/common/utils/HttpUtil.java |  184 +++++++++++++++++++++++++++++++++------------
 1 files changed, 135 insertions(+), 49 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..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
@@ -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 {
@@ -299,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