Why would URLConnection timeout after 6+ minutes instead of 5 seconds?

白昼怎懂夜的黑 提交于 2019-11-29 10:23:03

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.

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!