From cb38789006b411b29fba27a6a60493dc78946e17 Mon Sep 17 00:00:00 2001
From: cmg <527198563@qq.com>
Date: 星期六, 22 六月 2024 09:49:27 +0800
Subject: [PATCH] Merge branch 'master-stander' into master-jiayan

---
 src/main/java/com/hx/util/HttpMethodUtil.java |   76 +++++++++++++++++++++++++++++++++++++-
 1 files changed, 74 insertions(+), 2 deletions(-)

diff --git a/src/main/java/com/hx/util/HttpMethodUtil.java b/src/main/java/com/hx/util/HttpMethodUtil.java
index 0ce4fea..8bdd6a3 100644
--- a/src/main/java/com/hx/util/HttpMethodUtil.java
+++ b/src/main/java/com/hx/util/HttpMethodUtil.java
@@ -1,10 +1,12 @@
 package com.hx.util;
 
+import com.alibaba.fastjson.JSONObject;
 import org.apache.commons.io.IOUtils;
 
 import java.io.OutputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;
+import java.util.HashMap;
 import java.util.Map;
 
 /**
@@ -46,7 +48,7 @@
             con.setDoOutput(true);
             con.setDoInput(true);
             con.setUseCaches(false);
-            con.setReadTimeout(5000);
+            con.setReadTimeout(300000);
             con.setRequestProperty("Charset", "UTF-8");
             con.setRequestProperty("Content-Type", "application/json");
             if(header != null){
@@ -60,7 +62,77 @@
                 os.write(data.getBytes("utf-8"));
             }
 
-            if (con.getResponseCode() != 200){
+            if(HttpURLConnection.HTTP_OK != con.getResponseCode() && HttpURLConnection.HTTP_CREATED != con.getResponseCode()){
+                throw new RuntimeException("璇锋眰url澶辫触:"+con.getResponseCode());
+            }
+            // 璇诲彇杩斿洖鍐呭
+            wxMsgXml = IOUtils.toString(con.getInputStream(), "utf-8");
+            // //System.out.println("HttpURLUtil:"+wxMsgXml);
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (con != null) {
+                con.disconnect();
+            }
+        }
+        return wxMsgXml;
+    }
+
+    /**
+     * 璇锋眰http鍗忚 鑾峰彇淇℃伅宸ュ叿
+     * @param url 璇锋眰閾炬帴
+     * @param data 璇锋眰鏁版嵁(body)
+     * @param keyValues form琛ㄥ崟鏁版嵁 key鍙傛暟鍚嶇О锛寁alue鍙傛暟鍊�
+     * @param header 璇锋眰澶�
+     * @param requestMethod 璇锋眰澶存柟娉曪紝榛樿POST
+     * @return
+     */
+    public static String request(String url, String data,Map<String,Object> keyValues,Map<String,String> header,String requestMethod, String contentType) {
+        HttpURLConnection con = null;
+        URL u = null;
+        String wxMsgXml = null;
+        try {
+            StringBuilder dataP = new StringBuilder();
+            if (keyValues != null && !keyValues.isEmpty()) {
+                for (Map.Entry<String, Object> entry : keyValues.entrySet()) {
+                    dataP.append((String)entry.getKey()).append("=");
+                    dataP.append(entry.getValue());
+                    dataP.append("&");
+                }
+                System.out.println("dataP:"+dataP.toString());
+                dataP.deleteCharAt(dataP.length() - 1);
+                url = url+"?"+dataP;
+            }
+
+            if(StringUtils.isEmpty(requestMethod)){
+                requestMethod = "POST";
+            }
+
+            if(StringUtils.isEmpty(contentType)){
+                contentType = "application/json";
+            }
+
+            u = new URL(url);
+            con = (HttpURLConnection) u.openConnection();
+            con.setRequestMethod(requestMethod);
+            con.setDoOutput(true);
+            con.setDoInput(true);
+            con.setUseCaches(false);
+            con.setReadTimeout(300000);
+            con.setRequestProperty("Charset", "UTF-8");
+            con.setRequestProperty("Content-Type", contentType);
+            if(header != null){
+                for (Map.Entry<String, String> entry : header.entrySet()) {
+                    con.setRequestProperty(entry.getKey(),entry.getValue());
+                }
+            }
+
+            if (data != null) {
+                OutputStream os = con.getOutputStream();
+                os.write(data.getBytes("utf-8"));
+            }
+
+            if(HttpURLConnection.HTTP_OK != con.getResponseCode() && HttpURLConnection.HTTP_CREATED != con.getResponseCode()){
                 throw new RuntimeException("璇锋眰url澶辫触:"+con.getResponseCode());
             }
             // 璇诲彇杩斿洖鍐呭

--
Gitblit v1.8.0