Skip to main content

HttpURLConnection setReadTimeout is broken?

2 replies [Last post]
Joined: 2005-06-08

Using: jdk1.6.0_17
I am trying to develop something that can track an HTTP URL for availability.
I was almost ready to use Apache's http client library then I suddenly noticed that since 1.4 Java net API has setReadTimeout on HttpURLConnection
So I went ahead and built something that looks like this

HttpURLConnection httpConn = initHttpUrlConnection(); //initates connection to http url
OutputStream out = httpConn.getOutputStream();
out.write(testRequestData.getBytes());"Sent request to server");
int responseTimeOutSecs = 60 //will wait for 60 secs for response
httpConn.setConnectTimeout(responseTimeOutSecs * 1000);
httpConn.setReadTimeout(responseTimeOutSecs * 1000);

Now, as a test, I shut down the Server Application (not the app.server) that serves the page I am trying to read.

But the code just hangs at

InputStreamReader httpInputStreamReader =
new InputStreamReader(httpConn.getInputStream());

Instead of throwing a SocketTimeoutException as explained in the JavaDoc.

Have you seen similar behavior?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2005-05-10


Just a thought. Did you try to set the timeouts before any of the getInput/getOutputStream calls? I seem to remember that you need to set these before the socket is opened.

That said, I have had more success using the Apache HTTP Client...

Best regards,

Harald K

Joined: 2005-06-08

The answer is yes. Thanks for replying to my question anyway.
I am also looking at apache http client now.