KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have the following code block: try { URL url = new URL("http://site-to-test.com/nonexistingpage.html"); HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); urlc.setRequestProperty("User-Agent", CoreProtocolPNames.USER_AGENT); urlc.setRequestProperty("Connection", "close"); urlc.setConnectTimeout(500); // timeout is in milliseconds urlc.connect(); if (urlc.getResponseCode() == 404) { // Server was reachable Log.i(TAG, "Server is reachable"); } } catch (MalformedURLException mue) { Log.e(TAG, "MalformedURLException: " + mue.getMessage()); } catch (IOException e) { Log.e(TAG, "IOException: " + e.getMessage()); } When the site-to-test domain is not reachable through the current network, this code blocks for about 30-40 seconds before receiving the IOException . And I specifically set the timeout value to 500ms. What am I missing here? Shouldn't the above block terminate in half a second, regardless of the network state and the site's availability?
Tags (comma-separated)
Save Edits
Cancel