Why would a “java.net.ConnectException: Connection timed out” exception occur when URL is up?

后端 未结 10 1664
傲寒
傲寒 2020-11-29 18:42

I\'m getting a ConnectException: Connection timed out with some frequency from my code. The URL I am trying to hit is up. The same code works for some users,

相关标签:
10条回答
  • 2020-11-29 18:55

    This can be a IPv6 problem (the host publishes an IPv6 AAAA-Address and the users host thinks it is configured for IPv6 but it is actually not correctly connected). This can also be a network MTU problem, a firewall block, or the target host might publish different IP addresses (randomly or based on originators country) which are not all reachable. Or similliar network problems.

    You cant do much besides setting a timeout and adding good error messages (especially printing out the hosts' resolved address). If you want to make it more robust add retry, parallel trying of all addresses and also look into name resolution caching (positive and negative) on the Java platform.

    0 讨论(0)
  • 2020-11-29 18:55

    Why would a “java.net.ConnectException: Connection timed out” exception occur when URL is up?

    Because the URLConnection (HttpURLConnection/HttpsURLConnection) is erratic. You can read about this here and here. Our solution were two things:

    a) set the ContentLength via setFixedLengthStreamingMode

    b) catch any TimeoutException and retry if it failed.

    0 讨论(0)
  • 2020-11-29 18:57
    • try to do the Telnet to see any firewall issue
    • perform tracert/traceroute to find number of hops
    0 讨论(0)
  • 2020-11-29 19:03

    Connection timeouts (assuming a local network and several client machines) typically result from

    a) some kind of firewall on the way that simply eats the packets without telling the sender things like "No Route to host"

    b) packet loss due to wrong network configuration or line overload

    c) too many requests overloading the server

    d) a small number of simultaneously available threads/processes on the server which leads to all of them being taken. This happens especially with requests that take a long time to run and may combine with c).

    Hope this helps.

    0 讨论(0)
  • 2020-11-29 19:03

    If the URL works fine in the web browser on the same machine, it might be that the Java code isn't using the HTTP proxy the browser is using for connecting to the URL.

    0 讨论(0)
  • 2020-11-29 19:08

    The reason why this happened to me was that a remote server was allowing only certain IP addressed but not its own, and I was trying to render the images from the server's URLs... so everything would simply halt, displaying the timeout error that you had...

    Make sure that either the server is allowing its own IP, or that you are rendering things from some remote URL that actually exists.

    0 讨论(0)
提交回复
热议问题