[英]JAVA: What's the fastest and best way to get a response code from HTTPS URL?
private int getResponse(String url) throws Exception {
try {
URL check = new URL(url);
HttpsURLConnection connection = (HttpsURLConnection)check.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.connect();
return(connection.getResponseCode());
} catch (java.net.SocketTimeoutException e) {
return getResponse(url);
}
}
Is there a faster way to get the response code from a URL than HttpsURLConnection?有没有比 HttpsURLConnection 更快的从 URL 获取响应代码的方法?
I tried HeadMethod from the HTTP Client Commons but that's not that much faster.我尝试了 HTTP Client Commons 中的 HeadMethod,但这并没有那么快。
Thanks in advance提前致谢
I strongly suspect that differences will be absolutely minimal, when compared to normal internet delays and the network stack that Java itself is using (provided by the underlying OS).我强烈怀疑,与正常的互联网延迟和 Java 本身使用的网络堆栈(由底层操作系统提供)相比,差异绝对是最小的。
While making 1 request will incur in roughly the same overhead regardless of library (as long as it is not completely broken; both Apache Commons and JDK are ok), if you are going to make multiple requests, there are several things that will dramatically improve performance:虽然无论库如何,发出 1 个请求都会产生大致相同的开销(只要它没有完全损坏;Apache Commons 和 JDK 都可以),如果你要发出多个请求,有几件事会显着改善表现:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.