Reputation: 33
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?
I tried HeadMethod from the HTTP Client Commons but that's not that much faster.
Thanks in advance
Upvotes: 2
Views: 2702
Reputation: 17945
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).
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:
Upvotes: 2