gpt4 book ai didi

java - 来自服务器的响应代码

转载 作者:行者123 更新时间:2023-12-01 15:08:04 25 4
gpt4 key购买 nike

public static boolean testConnection() {
try {
// TODO add your handling code here:
System.setProperty("http.proxyHost", "some proxy name");
System.setProperty("http.proxyPort", "some port");
URL a = new URL("http://" + Variables.serverName + ":" + Variables.serverPort + "/DeviceCloud");
urlString = a.toExternalForm()+"/";
System.out.println(urlString);
System.out.println("http://" + Variables.serverName + ":" + Variables.serverPort + "/DeviceCloud");
URLConnection conn = a.openConnection();
int respCode = ((HttpURLConnection) conn).getResponseCode();
System.out.println(respCode);
if (respCode >= 500) {
return false;
} else {
return true;
}
} catch (Exception ex) {
return false;
}
}

如果服务器可以访问,它就可以正常工作。但如果服务器无法访问意味着需要很长时间。它没有显示任何输出。但实际上服务器计算机也在从客户端执行 ping 操作。什么是获得状态的正确解决方案

最佳答案

您可以使用 setConnectTimeout() 设置超时方法:

try {
HttpURLConnection httpconn = (HttpURLConnection) a.openConnection();
httpconn.setConnectTimeout(10000); //10 seconds timeout

return (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (SocketTimeoutException e) {
// You can get an output here if it timed out
return false;
}

关于java - 来自服务器的响应代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12709524/

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