HttpURLConnection TimeOut Java

by janani 2014-03-08 19:52:58


HttpURLConnection TimeOut:



The below code will return false if the URL takes more then 10 seconds to connect:


import java.net.HttpURLConnection;

try {
URL url = new URL("http://www.google.com");

HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();

HttpURLConnection.setFollowRedirects(false);

con.setRequestMethod("GET");

con.setConnectTimeout(10000); //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;

}

Tagged in:

1282
like
0
dislike
0
mail
flag

You must LOGIN to add comments