HttpURLConnection timeout settings
HttpURLConnection
has a setConnectTimeout method.
Just set the timeout to 5000 milliseconds, and then catch java.net.SocketTimeoutException
Your code should look something like this:
try {
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setRequestMethod("HEAD");
con.setConnectTimeout(5000); //set timeout to 5 seconds
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (java.net.SocketTimeoutException e) {
return false;
} catch (java.io.IOException e) {
return false;
}
How to make HttpURLConnection timeout?
I believe the connectTimeout
applies only to the connection itself, i.e. the period before the handshake is completed. For timeout waiting for the server to send the content, try connection.setReadTimeout(1);
.
HttpURLConnection timeout defaults
Appears the "default" timeouts for HttpURLConnection are zero which means "no timeout."
Unfortunately, in my experience, it appears using these defaults can lead to an unstable state, depending on what happens with your connection to the server. If you use an HttpURLConnection
and don't explicitly set (at least read) timeouts, your connection can get into a permanent stale state. By default. So always set setReadTimeout
to "something" or you might orphan connections (and possibly threads depending on how your app runs).
It appears from trial and error that calling setConnectTimeout
isn't required because the socket itself seems to have like a 2 minute "connect timeout" built in (at least in OS X).
You may also be able to set a "global default" for the timeouts by adjusting system properties.
Fix/prognosis: always set a readTimeout (even if very large), or use a different client that lets you set SO_KEEPALIVE. The default without these result in threads hanging "forever" without it (when/if they do a read), or on stale sockets sticking around forever...
HTTP URL Connection Timeout
You are sending a HTTP url through HttpsURLConnection. So its making problem. just change your first 2 line. You will get the proper output.
URL url = new URL("http://62.215.226.164/fccsms_P.aspx");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
Setting maximum timeout of java.net.URLConnection
You cannot increase the connect timeout beyond the platform default (contrary to 20 years of Javadoc), and there is no point in doing so. It has no bearing on download times. The only relevant timeout for that is the read timeout.
In any case, if the 'source URL is too slow', merely setting timeouts won't make it any faster.
Related Topics
How to Set the Jdk Netbeans Runs On
Convert Base64 String to Image
System.Currenttimemillis() VS. New Date() VS. Calendar.Getinstance().Gettime()
How to Programmatically Set the Sslcontext of a Jax-Ws Client
Dbcp - Validationquery for Different Databases
Java.Lang.Classcastexception Using Lambda Expressions in Spark Job on Remote Server
How to Find the Jvm Version from a Program
Java.Io.Ioexception: Mark/Reset Not Supported
Run Java Application at Windows Startup
Jpa: Unidirectional Many-To-One and Cascading Delete
Spring Cache @Cacheable - Not Working While Calling from Another Method of the Same Bean
How to Determine If a Date Is Between Two Dates in Java
Make Arraylist.Toarray() Return More Specific Types
Why F Is Placed After Float Values
Using Powermockito.Whennew() Is Not Getting Mocked and Original Method Is Called