From caf2599a9869244ded811018811c37a2aabac3fc Mon Sep 17 00:00:00 2001
From: vince <757871790@qq.com>
Date: 星期三, 08 一月 2025 11:29:05 +0800
Subject: [PATCH] 优化测温协议

---
 src/main/java/com/fzzy/gateway/util/GatewayHttpUtil.java |   59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 58 insertions(+), 1 deletions(-)

diff --git a/src/main/java/com/fzzy/gateway/util/GatewayHttpUtil.java b/src/main/java/com/fzzy/gateway/util/GatewayHttpUtil.java
index 3426d39..25846ce 100644
--- a/src/main/java/com/fzzy/gateway/util/GatewayHttpUtil.java
+++ b/src/main/java/com/fzzy/gateway/util/GatewayHttpUtil.java
@@ -9,6 +9,11 @@
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.util.EntityUtils;
 
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
 import java.net.URLEncoder;
 import java.util.Map;
 
@@ -17,6 +22,8 @@
  */
 @Slf4j
 public class GatewayHttpUtil {
+
+    private static final int TIMEOUT_IN_MILLIONS = 5000;
 
     /**
      * 鎵цGET璇锋眰
@@ -40,7 +47,7 @@
             }
             HttpGet method = new HttpGet(getUrl);
             method.setHeader("Accept", "application/json");
-            method.setHeader("charset", "UTF-8");
+            method.setHeader("charset", "utf-8");
 
             response = client.execute(method);
 //            response.setHeader("Accept", "application/json");
@@ -62,4 +69,54 @@
         return responseText;
     }
 
+    public static String doGet(String urlStr) {
+        URL url = null;
+        HttpURLConnection conn = null;
+        InputStream is = null;
+        ByteArrayOutputStream baos = null;
+        try {
+            url = new URL(urlStr);
+            conn = (HttpURLConnection) url.openConnection();
+            conn.setReadTimeout(TIMEOUT_IN_MILLIONS);
+            conn.setConnectTimeout(TIMEOUT_IN_MILLIONS);
+            conn.setRequestMethod("GET");
+            conn.setRequestProperty("accept", "*/*");
+            conn.setRequestProperty("connection", "Keep-Alive");
+            conn.setRequestProperty("Content-Type", "application/json");
+            if (conn.getResponseCode() == 200) {
+                is = conn.getInputStream();
+                baos = new ByteArrayOutputStream();
+                int len = -1;
+                byte[] buf = new byte[128];
+
+                while ((len = is.read(buf)) != -1) {
+                    baos.write(buf, 0, len);
+                }
+                baos.flush();
+                return baos.toString();
+            } else {
+                throw new RuntimeException(" responseCode is not 200 ... ");
+            }
+
+        } catch (Exception e) {
+            e.printStackTrace();
+
+        } finally {
+            try {
+                if (is != null)
+                    is.close();
+            } catch (IOException e) {
+            }
+            try {
+                if (baos != null)
+                    baos.close();
+            } catch (IOException e) {
+            }
+            conn.disconnect();
+        }
+
+        return null;
+
+    }
+
 }
\ No newline at end of file

--
Gitblit v1.9.3