From ca66de0b8e57f3c8c09ffa294751b36880c22ed3 Mon Sep 17 00:00:00 2001 From: fit2cloud-chenyw Date: Fri, 30 Aug 2024 13:46:09 +0800 Subject: [PATCH] =?UTF-8?q?fix(X-Pack):=20=E7=A4=BE=E5=8C=BA=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=90=AF=E5=8A=A8=E6=9C=89xpack=E6=8A=A5=E9=94=99?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=20#11317?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- de-xpack | 2 +- .../io/dataease/utils/HttpClientUtil.java | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/de-xpack b/de-xpack index 47251c7975..22554aaec0 160000 --- a/de-xpack +++ b/de-xpack @@ -1 +1 @@ -Subproject commit 47251c7975cbee373fd183a3426b3b24526d1d65 +Subproject commit 22554aaec016346ee0605f8e8923b1a858e1f1b7 diff --git a/sdk/common/src/main/java/io/dataease/utils/HttpClientUtil.java b/sdk/common/src/main/java/io/dataease/utils/HttpClientUtil.java index a8ef27cc28..79f799aed3 100755 --- a/sdk/common/src/main/java/io/dataease/utils/HttpClientUtil.java +++ b/sdk/common/src/main/java/io/dataease/utils/HttpClientUtil.java @@ -31,7 +31,10 @@ import org.slf4j.LoggerFactory; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.IOException; import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; import java.nio.charset.StandardCharsets; import java.security.cert.X509Certificate; import java.util.*; @@ -521,4 +524,28 @@ public class HttpClientUtil { } } } + + public static boolean isURLReachable(String urlString, Map head) { + try { + URL url = new URL(urlString); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setConnectTimeout(5000); // 设置连接超时时间,单位为毫秒 + connection.setReadTimeout(5000); // 设置读取超时时间,单位为毫秒 + if (MapUtils.isNotEmpty(head)) { + for (Map.Entry entry : head.entrySet()) { + connection.addRequestProperty(entry.getKey(), entry.getValue()); + } + } + int responseCode = connection.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { + return true; // 状态码200表示URL可达 + } else if (StringUtils.equalsIgnoreCase("Unauthorized", connection.getResponseMessage())) { + LogUtil.error("apisix key error [failed to check token]"); + } + } catch (IOException e) { + return false; + } + return false; // 如果发生异常或状态码不是200,则URL不可达 + } }