Java – SocketTimeoutException when ConnectTimeout and ReadTimeout is infinite?

androidhttpurlconnectionjavasocket-timeout-exception

Possible Duplicate:
Receiving request timeout even though connect timeout and read timeout is set to default (infinite)?

I tried to connect to a web service and received a SocketTimeoutException after approximately 20 seconds. The Tomcat server hosting the web service is down so the Exception is expected. However, I did not set the value of my ConnectTimeout and ReadTimeout. According to the documentation, the default values of these two are infinite.

One possibility for this is that the server I tried connecting to has its own timeout. But when my friend tried to connect to it using iOS, his connection timed out after approximately 1 minute and 15 seconds. If the server is the one issuing the timeout, our connection should have timed out at almost the same time. Please note that he is also using the default time out of iOS.

  • Why did my socket timed out so early when my connect and read timeout are set to infinite?
  • Is socket timeout different to connect and read timeout? If so, how is it different?
  • How can I know the value of my socket timeout? I am using HttpURLConnection.
  • Is there a way to set the socket timeout? How?

Below is a snippet of my code:

httpURLConnection = (HttpURLConnection) ((new URL("http://www.website.com/webservice")).openConnection());
httpURLConnection.setDoInput(isDoInput);
httpURLConnection.setDoOutput(isDoOutput);
httpURLConnection.setRequestMethod(method);

try
{
    OutputStreamWriter writer = new OutputStreamWriter(httpURLConnection.getOutputStream());
    writer.write("param1=value1");
    writer.flush;
}catch(Exception e)
{

}

Best Answer

Why did my socket timed out so early when my connect and read timeout are set to infinite?

Code please.

Is socket timeout different to connect and read timeout? If so, how is it different?

SocketTimeoutException is a read timeout.

How can I know the value of my socket timeout? I am using HttpURLConnection.

HttpURLConnection.getReadTimeout(); also HttpURLConnection.getConnectTimeout().

Is there a way to set the socket timeout? How?

HttpURLConnection.setReadTimeout().

You have already cited all these methods in your original post. Why are you asking about them here?