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

JavaConnectionUrlconnectionConnectexception

Java Problem Overview


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, but not others. It seems like once one user starts to get this exception they continue to get the exception.

Here is the stack trace:

java.net.ConnectException: Connection timed out
Caused by: java.net.ConnectException: Connection timed out
	at java.net.PlainSocketImpl.socketConnect(Native Method)
	at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
	at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
	at java.net.Socket.connect(Socket.java:516)
	at java.net.Socket.connect(Socket.java:466)
	at sun.net.NetworkClient.doConnect(NetworkClient.java:157)
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:365)
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:477)
	at sun.net.www.http.HttpClient.<init>(HttpClient.java:214)
	at sun.net.www.http.HttpClient.New(HttpClient.java:287)
	at sun.net.www.http.HttpClient.New(HttpClient.java:299)
	at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:796)
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:748)
	at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:673)
	at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:840)

Here is a snippet from my code:

URLConnection urlConnection = null;
OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
InputStream inputStream = null;
	
try {
	URL url = new URL(urlBase);
	urlConnection = url.openConnection();
	urlConnection.setDoOutput(true);

	outputStream = urlConnection.getOutputStream(); // exception occurs on this line
	outputStreamWriter = new OutputStreamWriter(outputStream);
	outputStreamWriter.write(urlString);
	outputStreamWriter.flush();
	inputStream = urlConnection.getInputStream();
	String response = IOUtils.toString(inputStream);
    return processResponse(urlString, urlBase, response);
} catch (IOException e) {
	throw new Exception("Error querying url: " + urlString, e);
} finally {
	IoUtil.close(inputStream);
	IoUtil.close(outputStreamWriter);
	IoUtil.close(outputStream);
}

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

The error message says it all: your connection timed out. This means your request did not get a response within some (default) timeframe. The reasons that no response was received is likely to be one of:

a) The IP/domain or port is incorrect

b) The IP/domain or port (i.e service) is down

c) The IP/domain is taking longer than your default timeout to respond

d) You have a firewall that is blocking requests or responses on whatever port you are using

e) You have a firewall that is blocking requests to that particular host

f) Your internet access is down

g) Your live-server is down i.e in case of "rest-API call".

Note that firewalls and port or IP blocking may be in place by your ISP

Solution 4 - Java

I'd recommend raising the connection timeout time before getting the output stream, like so:

urlConnection.setConnectTimeout(1000);

Where 1000 is in milliseconds (1000 milliseconds = 1 second).

Solution 5 - Java

  • try to do the Telnet to see any firewall issue
  • perform tracert/traceroute to find number of hops

Solution 6 - Java

I solved my problem with:

System.setProperty("https.proxyHost", "myProxy");
System.setProperty("https.proxyPort", "80");

or http.proxyHost...

Solution 7 - Java

> 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.

Solution 8 - Java

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.

Solution 9 - Java

There is a possibility that your IP/host are blocked by the remote host, especially if it thinks you are hitting it too hard.

Solution 10 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSarah HaskinsView Question on Stackoverflow
Solution 1 - JavaJumpyView Answer on Stackoverflow
Solution 2 - JavaAlexanderView Answer on Stackoverflow
Solution 3 - JavaM Hamza JavedView Answer on Stackoverflow
Solution 4 - JavaPowerlordView Answer on Stackoverflow
Solution 5 - Javashahbaz sikanderView Answer on Stackoverflow
Solution 6 - JavaNikNikView Answer on Stackoverflow
Solution 7 - JavaLonzakView Answer on Stackoverflow
Solution 8 - JavaeckesView Answer on Stackoverflow
Solution 9 - JavalarsiviView Answer on Stackoverflow
Solution 10 - Java3xCh1_23View Answer on Stackoverflow