From 70133cc2bbcf58ef760ee0c84f031dc66b7fb1c2 Mon Sep 17 00:00:00 2001
From: zhaowenxuan <chacca165@163.com>
Date: 星期二, 17 十二月 2024 21:36:21 +0800
Subject: [PATCH] 20241217

---
 ltkj-admin/src/main/java/com/ltkj/web/wxUtils/HttpClientUtils.java |  139 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 139 insertions(+), 0 deletions(-)

diff --git a/ltkj-admin/src/main/java/com/ltkj/web/wxUtils/HttpClientUtils.java b/ltkj-admin/src/main/java/com/ltkj/web/wxUtils/HttpClientUtils.java
index 910cde3..88623b0 100644
--- a/ltkj-admin/src/main/java/com/ltkj/web/wxUtils/HttpClientUtils.java
+++ b/ltkj-admin/src/main/java/com/ltkj/web/wxUtils/HttpClientUtils.java
@@ -26,10 +26,12 @@
 import java.io.*;
 import java.net.HttpURLConnection;
 import java.net.URL;
+import java.net.URLEncoder;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.UUID;
 
 
 /**
@@ -179,6 +181,143 @@
         return null;
     }
 
+    public static String sendPostTokenFormUrlencoded(String httpUrl, Map<String, Object> maps, String authorization) {
+        HttpURLConnection connection = null;
+        OutputStreamWriter writer = null;
+        BufferedReader reader = null;
+        StringBuilder response = new StringBuilder();
+
+        try {
+            // 鍒涘缓 URL 瀵硅薄
+            URL url = new URL(httpUrl);
+            // 鎵撳紑杩炴帴
+            connection = (HttpURLConnection) url.openConnection();
+
+            // 璁剧疆璇锋眰鏂规硶涓� POST
+            connection.setRequestMethod("POST");
+            // 璁剧疆璇锋眰澶� Content-Type 涓� application/x-www-form-urlencoded
+            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
+
+            // 濡傛灉闇�瑕� Authorization Header
+            if (authorization != null && !authorization.isEmpty()) {
+                connection.setRequestProperty("Authorization", authorization);
+            }
+
+            // 璁剧疆鍏佽杈撳嚭
+            connection.setDoOutput(true);
+
+            // 鏋勫缓琛ㄥ崟鏁版嵁
+            StringBuilder postData = new StringBuilder();
+            for (Map.Entry<String, Object> entry : maps.entrySet()) {
+                if (postData.length() > 0) {
+                    postData.append("&");
+                }
+                postData.append(URLEncoder.encode(entry.getKey(), "UTF-8"))
+                        .append("=")
+                        .append(URLEncoder.encode(entry.getValue().toString(), "UTF-8"));
+            }
+
+            // 鑾峰彇杈撳嚭娴佸苟鍐欏叆琛ㄥ崟鏁版嵁
+            writer = new OutputStreamWriter(connection.getOutputStream());
+            writer.write(postData.toString());
+            writer.flush();
+
+            // 鑾峰彇鍝嶅簲
+            reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+            String line;
+            while ((line = reader.readLine()) != null) {
+                response.append(line);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            return null;
+        } finally {
+            // 鍏抽棴娴�
+            try {
+                if (writer != null) writer.close();
+                if (reader != null) reader.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+            if (connection != null) {
+                connection.disconnect();
+            }
+        }
+        return response.toString();
+    }
+
+    public static String sendPostTokenFormData(String httpUrl, Map<String, Object> maps, String authorization) {
+        HttpURLConnection connection = null;
+        DataOutputStream outStream = null;
+        BufferedReader reader = null;
+        StringBuilder response = new StringBuilder();
+
+        // 杈圭晫瀛楃涓�
+        String boundary = "----WebKitFormBoundary" + UUID.randomUUID().toString().replaceAll("-", "");
+        String CRLF = "\r\n"; // 鎹㈣绗�
+
+        try {
+            // 鍒涘缓 URL 瀵硅薄
+            URL url = new URL(httpUrl);
+            // 鎵撳紑杩炴帴
+            connection = (HttpURLConnection) url.openConnection();
+
+            // 璁剧疆璇锋眰鏂规硶涓� POST
+            connection.setRequestMethod("POST");
+            // 璁剧疆璇锋眰澶� Content-Type 涓� multipart/form-data
+            connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
+
+            // 濡傛灉闇�瑕� Authorization Header
+            if (authorization != null && !authorization.isEmpty()) {
+                connection.setRequestProperty("Authorization", authorization);
+            }
+
+            // 璁剧疆鍏佽杈撳嚭
+            connection.setDoOutput(true);
+
+            // 鑾峰彇杈撳嚭娴�
+            outStream = new DataOutputStream(connection.getOutputStream());
+
+            // 閬嶅巻浼犲叆鐨勮〃鍗曟暟鎹紝骞舵寜鐓� multipart/form-data 鏍煎紡鍐欏叆
+            for (Map.Entry<String, Object> entry : maps.entrySet()) {
+                outStream.writeBytes("--" + boundary + CRLF);
+                outStream.writeBytes("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + CRLF);
+                outStream.writeBytes("Content-Type: text/plain; charset=UTF-8" + CRLF);
+                outStream.writeBytes(CRLF);  // 杩欓噷鏄垎闅旂锛岀┖涓�琛�
+                outStream.writeBytes(entry.getValue().toString() + CRLF);
+            }
+
+            // 缁撴潫 multipart/form-data 鍙戦�佹暟鎹儴鍒�
+            outStream.writeBytes("--" + boundary + "--" + CRLF);
+
+            // 鍒锋柊杈撳嚭娴�
+            outStream.flush();
+
+            // 鑾峰彇鍝嶅簲
+            reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+            String line;
+            while ((line = reader.readLine()) != null) {
+                response.append(line);
+            }
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            return null;
+        } finally {
+            // 鍏抽棴娴�
+            try {
+                if (outStream != null) outStream.close();
+                if (reader != null) reader.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+            if (connection != null) {
+                connection.disconnect();
+            }
+        }
+        return response.toString();
+    }
+
     /**
      * 鍙戦�� post璇锋眰
      *

--
Gitblit v1.8.0