From aa93e512a60379c6373d10023f49dae4403b51c4 Mon Sep 17 00:00:00 2001
From: zhaowenxuan <chacca165@163.com>
Date: 星期五, 21 二月 2025 17:55:09 +0800
Subject: [PATCH] 金堆成同步字典服务

---
 src/main/java/com/example/utils/HttpClientUtils.java |  105 +++++++++++++++++++++++++++++-----------------------
 1 files changed, 59 insertions(+), 46 deletions(-)

diff --git a/src/main/java/com/example/utils/HttpClientUtils.java b/src/main/java/com/example/utils/HttpClientUtils.java
index f0a50d5..6cb36c2 100644
--- a/src/main/java/com/example/utils/HttpClientUtils.java
+++ b/src/main/java/com/example/utils/HttpClientUtils.java
@@ -97,7 +97,54 @@
     }
 
 
-    public static String sendPost(String httpUrl, Map<String, Object> maps) {
+    public static String sendPost(String httpUrl, Map<String, Object> maps,Map<String ,Object> headers) {
+
+        try {
+            // 1. 鍒涘缓 URL 瀵硅薄
+            URL url = new URL(httpUrl);
+            // 2. 鍒涘缓 HttpURLConnection 瀵硅薄
+            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+            // 3. 璁剧疆璇锋眰鏂规硶涓� POST
+            connection.setRequestMethod("POST");
+            // 4. 璁剧疆 Content-Type 澶撮儴瀛楁
+            connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
+            if (headers != null && !headers.isEmpty()){
+                for (Map.Entry<String, Object> entry : headers.entrySet()) {
+                    connection.setRequestProperty(entry.getKey(), entry.getValue().toString());
+                }
+            }
+            // 6. 鍚戞湇鍔″櫒鍙戦�佹暟鎹�
+            String requestBody = JSONUtil.toJsonStr(maps);
+            log.info(httpUrl+"鍏ュ弬:   "+requestBody);
+            log.info("璇锋眰澶� ->{}",JSONUtil.toJsonStr(headers));
+//            String requestBody1 = maps.toString();
+            byte[] postData = requestBody.getBytes(StandardCharsets.UTF_8);
+            connection.setDoOutput(true);
+            try (OutputStream outputStream = connection.getOutputStream()) {
+                outputStream.write(postData);
+            }
+
+            // 8. 鑾峰彇鍝嶅簲鏁版嵁
+            try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
+                String line;
+                StringBuilder response = new StringBuilder();
+                while ((line = reader.readLine()) != null) {
+                    response.append(line);
+                }
+                log.info("=====================================================");
+                log.info(httpUrl+"鍑哄弬");
+                log.info(response.toString());
+                connection.disconnect();
+                return response.toString();
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+        return null;
+    }
+
+    public static String sendPost(String httpUrl, String json) {
 
         try {
             // 1. 鍒涘缓 URL 瀵硅薄
@@ -109,10 +156,10 @@
             // 4. 璁剧疆 Content-Type 澶撮儴瀛楁
             connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
             // 6. 鍚戞湇鍔″櫒鍙戦�佹暟鎹�
-            String requestBody = JSONUtil.toJsonStr(maps);
+            String requestBody = json;
             log.info(httpUrl+"鍏ュ弬:   "+requestBody);
 //            String requestBody1 = maps.toString();
-            byte[] postData = requestBody.getBytes("UTF-8");
+            byte[] postData = requestBody.getBytes(StandardCharsets.UTF_8);
             connection.setDoOutput(true);
             try (OutputStream outputStream = connection.getOutputStream()) {
                 outputStream.write(postData);
@@ -185,28 +232,18 @@
         OutputStreamWriter writer = null;
         BufferedReader reader = null;
         StringBuilder response = new StringBuilder();
-
         try {
-            // 鍒涘缓 URL 瀵硅薄
+            log.info("璇锋眰鍦板潃 ->{}",httpUrl);
             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();
+            log.info("鍏ュ弬 ->{}", JSONUtil.toJsonStr(maps));
             for (Map.Entry<String, Object> entry : maps.entrySet()) {
                 if (postData.length() > 0) {
                     postData.append("&");
@@ -215,23 +252,20 @@
                         .append("=")
                         .append(URLEncoder.encode(entry.getValue().toString(), "UTF-8"));
             }
-
-            // 鑾峰彇杈撳嚭娴佸苟鍐欏叆琛ㄥ崟鏁版嵁
+            log.info("鍙傛暟鎷兼帴 ->{}",postData);
             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);
             }
+            log.info("杩斿洖 ->{}",response);
         } catch (Exception e) {
             e.printStackTrace();
             return null;
         } finally {
-            // 鍏抽棴娴�
             try {
                 if (writer != null) writer.close();
                 if (reader != null) reader.close();
@@ -250,60 +284,39 @@
         DataOutputStream outStream = null;
         BufferedReader reader = null;
         StringBuilder response = new StringBuilder();
-
-        // 杈圭晫瀛楃涓�
         String boundary = "----WebKitFormBoundary" + UUID.randomUUID().toString().replaceAll("-", "");
-        String CRLF = "\r\n"; // 鎹㈣绗�
-
+        String CRLF = "\r\n";
         try {
-            // 鍒涘缓 URL 瀵硅薄
+            log.info("璇锋眰鍦板潃 ->{}",httpUrl);
             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);
-
-            // 鑾峰彇杈撳嚭娴�
+            log.info("鍏ュ弬 ->{}",maps);
             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(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);
             }
-
+            log.info("杩斿洖 ->{}",response);
         } catch (Exception e) {
             e.printStackTrace();
             return null;
         } finally {
-            // 鍏抽棴娴�
             try {
                 if (outStream != null) outStream.close();
                 if (reader != null) reader.close();

--
Gitblit v1.8.0