我正在尝试找出如何检查 url 是否为 404'ing 或是否超时的逻辑。我似乎无法弄清楚!
这是我到目前为止所拥有的:
while (i < retries){
try {
response = Jsoup.connect(url)
.userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
.timeout(10000)
.execute();
success = true;
break;
} catch (IOException e) {
success = false;
}
System.out.println("Attempt " + i + " " + url);
i++;
}
}
public int getUrlStatus(){
if(success){
int statusCode = response.statusCode();
return statusCode;
}else {
return 404;
}
}
据我所知,这将告诉我页面 404 并不是页面超时。我该如何检查?
最佳答案
连接时可以捕获SocketTimeoutException
:
try {
response = Jsoup.connect(url)
.userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
.timeout(10000)
.execute();
success = true;
break;
} catch (SocketTimeoutExceptione) {
success = false;
System.out.println("Timeout occured");
}
关于java - Jsoup,确定超时和 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10602591/