3

The Apache HttpClient framework allows you to specify a connection timeout and a socket timeout, like so:

final HttpParams httpParams = new BasicHttpParams();
if (connectionTimeout > 0) {
    HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeout);
}
if (socketTimeout > 0) {
    HttpConnectionParams.setSoTimeout(httpParams, socketTimeout);
}
HttpClient client = new DefaultHttpClient(httpParams);

I would like to fine-tune my connection timeouts. This question explains the difference between the settings nicely, but provides no help for debugging the exceptions that get thrown.

My question is, what exception gets thrown for each type of timeout? I have a java.net.SocketTimeoutException; is this from a connection timeout or a socket timeout? I suspect it's the latter, especially since the message says "Read timed out". What exception will be thrown for a connection timeout? TimeoutException? ConnectException? Why aren't the relationships between timeout settings and exceptions thrown captured in the Apache Http Docs?

The direct parent classes for SocketTimeoutException are InterruptedIoException and IOException; the former has only the SocketTimeoutException for a subclass, but the IOException has many subclasses. Is there another reference that covers what all flavors of exceptions one can expect to receive from an HttpClient execute method? It only declares that it can throw an IOException, which is really very broad.

Community
  • 1
  • 1
Patrick M
  • 10,547
  • 9
  • 68
  • 101

1 Answers1

4

I haven't gone thru the relationship between classes in Java API, but I think what you need is

  1. java.net.ConnectException : Packet loss due to wrong network, network overload, too many request to server, firewall.

  2. java.net.SocketTimeoutException : The socket timeout is the amount of time to keep the server socket open while data is being transferred back to the caller. This could even be the server is still processing and writing back data but it's taking rather long and the client has just timed out waiting for it.

Himalay Majumdar
  • 3,883
  • 14
  • 65
  • 94