7

I am copying this method verbatim from my application, which isn't complete yet but it does attempt to provide me with a timeout stack trace if things don't go smoothly:

protected boolean isHttpAlive()  {
  boolean isHttpOk = false;

  HttpURLConnection httpConnection = null;
  try {
    URL gurl = new URL("http://www.amazon.com/");
    URLConnection connection = gurl.openConnection();
    connection.setConnectTimeout(5 * 1000); // 5 seconds!
    httpConnection = (HttpURLConnection) connection;
    int responseCode = httpConnection.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_OK)
      isHttpOk = true;
  } 
  catch (Exception e) {                    
    e.printStackTrace();
  }
  finally {
    if (httpConnection != null)
      httpConnection.disconnect();
  }

  return isHttpOk;
}

Now on one of my test devices (Droid), when there is a problem, I do get the exception but only after 6 minutes and 36 seconds, not 5 seconds as I set in the code above.

The timeout exception is thrown for the getResponseCode().

Why?

What am I missing?

Regex Rookie
  • 10,432
  • 15
  • 54
  • 88
  • 3
    My best guess is that the URL you are connecting to, Amazon in this case, has multiple IP addresses. As per the warning in the documentation " if the hostname resolves to multiple IP addresses, this client will try each in RFC 3484 order. If connecting to each of these addresses fails, multiple timeouts will elapse before the connect attempt throws an exception. Host names that support both IPv6 and IPv4 always have at least 2 IP addresses." – jnthnjns Jul 20 '12 at 15:55
  • @Asok I think you nailed it. Please post as an answer, highlighting **multiple timeouts will elapse before the connect attempt throws an exception**, so that I can accept. – Regex Rookie Jul 20 '12 at 16:18
  • what i hv observed is the timeout occurs at multiples of 60sec and your 5 sec may not work. – sachy Jul 20 '12 at 16:22

2 Answers2

5

My best guess is that the URL you are connecting to, Amazon in this case, has multiple IP addresses.

As per the warning in the documentation:

if the hostname resolves to multiple IP addresses, this client will try each in RFC 3484 order. If connecting to each of these addresses fails, multiple timeouts will elapse before the connect attempt throws an exception. Host names that support both IPv6 and IPv4 always have at least 2 IP addresses.


Edit:
I am still researching this because I'd like to convert my apps from HTTPClient to URLConnection. I am not satisfied with, in your instance, a 6+ minute timeout.

I did also stumble across this blog. He suggests adding connection.setReadTimeout(READ_TIMEOUT_MILLISECONDS); as well, don't know if that'll help your case.

jnthnjns
  • 8,962
  • 4
  • 42
  • 65
-1

this king of code is not very usefull to test connection. Reasons for a bad connection are numerous (each layer of the ip stack can have a problem). An example : you can have a connection and receive one byte per second, no timeout but not very fun for the user... and if you use "www.amazon.com" as test, they can play a joke to you (i'ld do that myself ;D)

divol
  • 192
  • 7
  • Anyway, You 'ld have a look to the Reachability class available on Apple's developper site. it's give you helper messages to manage network reachability. – divol Jul 22 '12 at 09:32