From 98ee045ccc0396c8d114b7c6da32940eb08d126f Mon Sep 17 00:00:00 2001
From: fwq <582742538@qq.com>
Date: 星期六, 10 十二月 2022 11:55:09 +0800
Subject: [PATCH] http请求增加超时状态

---
 src/main/java/com/hz/util/http/HttpHzUtil.java         |   54 +++++++++++++++++++++++++++++++++---------------------
 src/main/java/com/hz/util/http/dto/HttpHzResponse.java |    2 ++
 2 files changed, 35 insertions(+), 21 deletions(-)

diff --git a/src/main/java/com/hz/util/http/HttpHzUtil.java b/src/main/java/com/hz/util/http/HttpHzUtil.java
index ee9ef00..1f8e24e 100644
--- a/src/main/java/com/hz/util/http/HttpHzUtil.java
+++ b/src/main/java/com/hz/util/http/HttpHzUtil.java
@@ -6,25 +6,32 @@
 
 import java.io.OutputStream;
 import java.net.HttpURLConnection;
+import java.net.SocketTimeoutException;
 import java.net.URL;
 import java.util.Iterator;
 import java.util.Map;
 
 public class HttpHzUtil {
 
-    /**璇锋眰鏂规硶*/
-    public  static final String METHOD_POST = "POST";
-    /**缂栫爜*/
-    public  static final String CHARSET = "UTF-8";
+    /**
+     * 璇锋眰鏂规硶
+     */
+    public static final String METHOD_POST = "POST";
+    /**
+     * 缂栫爜
+     */
+    public static final String CHARSET = "UTF-8";
 
 
-    /**璇锋眰
-     * @param url 璇锋眰閾炬帴
-     * @param bodyData 璇锋眰body鏁版嵁
-     * @param keyValues 杩炴帴鎼哄甫鍙傛暟
-     * @param header 琛ㄥご鎼哄甫鍙傛暟
+    /**
+     * 璇锋眰
+     *
+     * @param url           璇锋眰閾炬帴
+     * @param bodyData      璇锋眰body鏁版嵁
+     * @param keyValues     杩炴帴鎼哄甫鍙傛暟
+     * @param header        琛ㄥご鎼哄甫鍙傛暟
      * @param requestMethod 璇锋眰绫诲瀷锛岄粯璁わ細POST
-     * @param outTime 瓒呮椂鏃堕棿锛堟绉掞級,榛樿锛�300000
+     * @param outTime       瓒呮椂鏃堕棿锛堟绉掞級,榛樿锛�300000
      * @return
      */
     public static HttpHzResponse HttpURLUtilJson(String url, String bodyData, Map<String, Object> keyValues, Map<String, String> header, String requestMethod, Integer outTime) {
@@ -37,9 +44,9 @@
             if (keyValues != null && !keyValues.isEmpty()) {
                 var9 = keyValues.entrySet().iterator();
 
-                while(var9.hasNext()) {
-                    entry = (Map.Entry)var9.next();
-                    dataP.append((String)entry.getKey()).append("=");
+                while (var9.hasNext()) {
+                    entry = (Map.Entry) var9.next();
+                    dataP.append((String) entry.getKey()).append("=");
                     dataP.append(entry.getValue());
                     dataP.append("&");
                 }
@@ -52,12 +59,12 @@
                 requestMethod = METHOD_POST;
             }
 
-            if(outTime == null || outTime < 0){
+            if (outTime == null || outTime < 0) {
                 outTime = 300000;
             }
 
             URL httpUrl = new URL(url);
-            con = (HttpURLConnection)httpUrl.openConnection();
+            con = (HttpURLConnection) httpUrl.openConnection();
             con.setRequestMethod(requestMethod);
             con.setDoOutput(true);
             con.setDoInput(true);
@@ -68,9 +75,9 @@
             if (header != null) {
                 var9 = header.entrySet().iterator();
 
-                while(var9.hasNext()) {
-                    entry = (Map.Entry)var9.next();
-                    con.setRequestProperty((String)entry.getKey(), (String)entry.getValue());
+                while (var9.hasNext()) {
+                    entry = (Map.Entry) var9.next();
+                    con.setRequestProperty((String) entry.getKey(), (String) entry.getValue());
                 }
             }
 
@@ -79,15 +86,20 @@
                 os.write(bodyData.getBytes(CHARSET));
             }
 
-            httpHzResponse.setCode(con.getResponseCode()+"");
-            if(con.getErrorStream() != null){
+            httpHzResponse.setCode(con.getResponseCode() + "");
+            if (con.getErrorStream() != null) {
                 httpHzResponse.setMsg(IOUtils.toString(con.getErrorStream(), CHARSET));
             }
-            if(HttpURLConnection.HTTP_OK == con.getResponseCode() || HttpURLConnection.HTTP_CREATED == con.getResponseCode()){
+            if (HttpURLConnection.HTTP_OK == con.getResponseCode() || HttpURLConnection.HTTP_CREATED == con.getResponseCode()) {
                 httpHzResponse.setData(IOUtils.toString(con.getInputStream(), CHARSET));
             }
 
+        }catch (SocketTimeoutException var15) {
+            httpHzResponse.setMsg(var15.getMessage());
+            httpHzResponse.setCode(HttpHzResponse.CODE_TIME_OUT);
+            var15.printStackTrace();
         } catch (Exception var14) {
+            httpHzResponse.setMsg(var14.getMessage());
             var14.printStackTrace();
         } finally {
             if (con != null) {
diff --git a/src/main/java/com/hz/util/http/dto/HttpHzResponse.java b/src/main/java/com/hz/util/http/dto/HttpHzResponse.java
index 2efbe3b..036ba99 100644
--- a/src/main/java/com/hz/util/http/dto/HttpHzResponse.java
+++ b/src/main/java/com/hz/util/http/dto/HttpHzResponse.java
@@ -18,6 +18,8 @@
     public static final String CODE_200 = "200";
     /**璇锋眰杩斿洖鐘舵��-鎴愬姛*/
     public static final String CODE_201 = "201";
+    /**璇锋眰鐘舵��-瓒呮椂*/
+    public static final String CODE_TIME_OUT = "time_out";
 
     /**灏佽鏍¢獙璇锋眰鐮佹柟娉曪紝娌℃湁杩涜閿欒鎻愮ず*/
     public boolean checkRequesCode(){

--
Gitblit v1.8.0