gpt4 book ai didi

java - 在 java 中 Ping url 并获取状态

转载 作者:行者123 更新时间:2023-12-01 19:16:34 24 4
gpt4 key购买 nike

         URLPing urlPing = new URLPing();
PingResponse pingResponse = urlPing.ping(URLName);
if(pingResponse.getResponseCode() == 200){
response = true;
}
else{
response=false;
}

这是我目前尝试过的。

最佳答案

Ping URL from JAVA Code

public static boolean pingUrl(final String address) {
try {
final URL url = new URL("http://" + address);
final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setConnectTimeout(1000 * 10); // mTimeout is in seconds
final long startTime = System.currentTimeMillis();
urlConn.connect();
final long endTime = System.currentTimeMillis();
if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("Time (ms) : " + (endTime - startTime));
System.out.println("Ping to "+address +" was success");
return true;
}
} catch (final MalformedURLException e1) {
e1.printStackTrace();
} catch (final IOException e) {
e.printStackTrace();
}
return false;
}

关于java - 在 java 中 Ping url 并获取状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6381306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com