From 9155df2aeab5b0d95c89dd928dbaefbfbaeae9d1 Mon Sep 17 00:00:00 2001
From: zjh <1084500556@qq.com>
Date: 星期四, 05 九月 2024 17:10:35 +0800
Subject: [PATCH] zjh 2024-09-05

---
 ltkj-admin/src/main/java/com/ltkj/web/wxUtils/HttpClientUtils.java |   76 ++++++++++++++++++++++++++++++++++----
 1 files changed, 68 insertions(+), 8 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 6b8b492..2299525 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
@@ -1,6 +1,8 @@
 package com.ltkj.web.wxUtils;
 
 
+import cn.hutool.json.JSONUtil;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
 import org.apache.http.NameValuePair;
@@ -21,9 +23,10 @@
 import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.util.EntityUtils;
 import org.springframework.stereotype.Component;
-
 import java.io.*;
+import java.net.HttpURLConnection;
 import java.net.URL;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -31,9 +34,11 @@
 
 /**
  * httpClient 宸ュ叿绫�
+ *
  * @create 2019-02-10 涓嬪崍 2:49
  */
 @Component
+@Slf4j
 public class HttpClientUtils {
 
     /**
@@ -46,20 +51,24 @@
 
     /**
      * 闈欐�佸唴閮ㄧ被---浣滅敤锛氬崟渚嬩骇鐢熺被鐨勫疄渚�
-     * @author Administrator
      *
+     * @author Administrator
      */
     private static class LazyHolder {
         private static final HttpClientUtils INSTANCE = new HttpClientUtils();
 
     }
-    HttpClientUtils(){}
-    public static HttpClientUtils getInstance(){
+
+    HttpClientUtils() {
+    }
+
+    public static HttpClientUtils getInstance() {
         return LazyHolder.INSTANCE;
     }
 
     /**
      * 鍙戦�� post璇锋眰
+     *
      * @param httpUrl 鍦板潃
      */
     public String sendHttpPost(String httpUrl) {
@@ -69,8 +78,9 @@
 
     /**
      * 鍙戦�� post璇锋眰
+     *
      * @param httpUrl 鍦板潃
-     * @param params 鍙傛暟(鏍煎紡:key1=value1&key2=value2)
+     * @param params  鍙傛暟(鏍煎紡:key1=value1&key2=value2)
      */
     public String sendHttpPost(String httpUrl, String params) {
         HttpPost httpPost = new HttpPost(httpUrl);// 鍒涘缓httpPost
@@ -85,10 +95,53 @@
         return sendHttpPost(httpPost);
     }
 
+
+    public static String sendPost(String httpUrl, Map<String, Object> maps) {
+
+        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");
+            // 6. 鍚戞湇鍔″櫒鍙戦�佹暟鎹�
+            String requestBody = JSONUtil.toJsonStr(maps);
+            log.info(httpUrl+"鍏ュ弬:   "+requestBody);
+//            String requestBody1 = maps.toString();
+            byte[] postData = requestBody.getBytes("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;
+    }
+
     /**
      * 鍙戦�� post璇锋眰
+     *
      * @param httpUrl 鍦板潃
-     * @param maps 鍙傛暟
+     * @param maps    鍙傛暟
      */
     public String sendHttpPost(String httpUrl, Map<String, String> maps) {
         HttpPost httpPost = new HttpPost(httpUrl);// 鍒涘缓httpPost
@@ -105,8 +158,10 @@
         return sendHttpPost(httpPost);
     }
 
+
     /**
      * 鍙戦�丳ost璇锋眰
+     *
      * @param httpPost
      * @return
      */
@@ -123,12 +178,12 @@
             long execStart = System.currentTimeMillis();
             response = httpClient.execute(httpPost);
             long execEnd = System.currentTimeMillis();
-            System.out.println("=================鎵цpost璇锋眰鑰楁椂锛�"+(execEnd-execStart)+"ms");
+            System.out.println("=================鎵цpost璇锋眰鑰楁椂锛�" + (execEnd - execStart) + "ms");
             long getStart = System.currentTimeMillis();
             entity = response.getEntity();
             responseContent = EntityUtils.toString(entity, "UTF-8");
             long getEnd = System.currentTimeMillis();
-            System.out.println("=================鑾峰彇鍝嶅簲缁撴灉鑰楁椂锛�"+(getEnd-getStart)+"ms");
+            System.out.println("=================鑾峰彇鍝嶅簲缁撴灉鑰楁椂锛�" + (getEnd - getStart) + "ms");
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
@@ -149,6 +204,7 @@
 
     /**
      * 鍙戦�� get璇锋眰
+     *
      * @param httpUrl
      */
     public String sendHttpGet(String httpUrl) {
@@ -158,6 +214,7 @@
 
     /**
      * 鍙戦�� get璇锋眰Https
+     *
      * @param httpUrl
      */
     public String sendHttpsGet(String httpUrl) {
@@ -167,6 +224,7 @@
 
     /**
      * 鍙戦�丟et璇锋眰
+     *
      * @param httpGet
      * @return
      */
@@ -206,6 +264,7 @@
 
     /**
      * 鍙戦�丟et璇锋眰Https
+     *
      * @param httpGet
      * @return
      */
@@ -244,6 +303,7 @@
 
     /**
      * 鍙戦�亁ml鏁版嵁
+     *
      * @param url
      * @param xmlData
      * @return

--
Gitblit v1.8.0