6+分钟而不是5秒后为什么会URLConnection的超时? [英] Why would URLConnection timeout after 6+ minutes instead of 5 seconds?

查看:150
本文介绍了6+分钟而不是5秒后为什么会URLConnection的超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我逐字从我的应用程序,它尚未完成复制此方法,但它确实试图给我提供一个超时的堆栈跟踪,如果事情不顺利:

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;
}

现在对我的测试设备一(Droid的),当有一个问题,我得到<一href="http://developer.android.com/reference/java/net/URLConnection.html#setConnectTimeout%28int%29">the除了但只有在<强6分36秒,不是5秒钟我在code以上设置。

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.

超时抛出异常的 GETRESPONSE code()

为什么?

我在想什么?

推荐答案

我最好的猜测是,你正在连接,亚马逊在这种情况下,URL,有多个IP地址。

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

由于按照文档

如果主机名解析为多个IP地址,此客户端将尝试每个在RFC 3484的顺序。如果连接到这些地址失败,多个超时将经过之前的连接尝试将引发异常。同时支持IPv6和IPv4主机名始终至少有2个IP地址。

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.

编辑:
我仍然在研究这个,因为我想从了HTTPClient 将我的应用程序,以的URLConnection 。我并不满足,在你的情况下,一个6+分钟超时。



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.

我也没穿过这个博客 。他建议增加 connection.setReadTimeout(READ_TIMEOUT_MILLISECONDS); 为好,不知道这会帮助你的情况

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.

这篇关于6+分钟而不是5秒后为什么会URLConnection的超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆