Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timeout in DefaultHttpClient

I'm a little confused on how the timeouts in DefaultHttpClient work.

I'm using this code:

private DefaultHttpClient createHttpClient() {
        HttpParams my_httpParams = new BasicHttpParams();

        HttpConnectionParams.setConnectionTimeout(my_httpParams, 3000);
        HttpConnectionParams.setSoTimeout(my_httpParams, 15000);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        ThreadSafeClientConnManager multiThreadedConnectionManager = new ThreadSafeClientConnManager(my_httpParams, registry);

        DefaultHttpClient httpclient = new DefaultHttpClient(multiThreadedConnectionManager, my_httpParams);

        return httpclient;
}

.

String url = "http://www.example.com";

DefaultHttpClient httpclient = createHttpClient();
HttpGet httpget = new HttpGet(url);

try {
    HttpResponse response = httpclient.execute(httpget);
    StatusLine statusLine = response.getStatusLine();
    mStatusCode = statusLine.getStatusCode();

    if (mStatusCode == 200){
        content = EntityUtils.toString(response.getEntity());
    }

} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (IllegalStateException e){
    e.printStackTrace();
}

When 15 seconds have passed and not all data has been received, an exception will be thrown, right? But on which method? I thought it to be the .execute(httpget) method but that one only tells me it throws ClientProtocolException and IOException. Could anyone help me clearifying this?

like image 980
Xander Avatar asked Apr 18 '13 16:04

Xander


People also ask

What is read timeout in HTTP client?

timeout) – the time waiting for data – after establishing the connection; maximum time of inactivity between two data packets. the Connection Manager Timeout (http. connection-manager. timeout) – the time to wait for a connection from the connection manager/pool.

What is HTTP socket timeout?

http.socket.timeout. The default socket timeout ( SO_TIMEOUT ) in milliseconds which is the timeout for waiting for data. A timeout value of zero is interpreted as an infinite timeout. This value is used when no socket timeout is set in the HTTP method parameters. Type: Integer.

How do I set request timeout?

In the Request timeout field, enter the timeout value that you want to use in seconds. Use values ranging from 1 to 3600 seconds, or from 1 to 60 minutes. Click Create or Deploy.

What is a good HTTP timeout?

Knowing the time available to provide a response can avoid problems with timeouts. Current implementations select times between 30 and 120 seconds, times that have been empirically determined to be safe.


2 Answers

It does throw an exception on execute(). The parent of SocketTimeoutException is an IOException. A catch block handling IOException will be able to catch both.

Try executing this code.

HttpParams my_httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(my_httpParams, 3000);
HttpConnectionParams.setSoTimeout(my_httpParams, 1);
DefaultHttpClient defaultHttpClient = new DefaultHttpClient(my_httpParams);
HttpGet httpGet = new HttpGet("http://google.com");
defaultHttpClient.execute(httpGet);

It results in this exception.

java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(Unknown Source)
    ...
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)

You can always choose to selectively process the exception by catching it and handling IOException later.

try
{
    // Your code
}
catch (SocketTimeoutException e)
{
    // handle timeouts
    e.printStackTrace();
}
catch (IOException e)
{
    // handle other IO exceptions
    e.printStackTrace();
}
like image 101
Deepak Bala Avatar answered Oct 10 '22 18:10

Deepak Bala


If you look at the Apache docs at http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e249, it notes that connection timeout exceptions are IOException subclasses.

To be more specific, I believe they'll either ConnectTimeoutExceptions if the connection can't be set up within your configured connection timeout, or SocketTimeoutExceptions, if it's set up but no data is received for your configured SO timeout.

like image 28
Tim Perry Avatar answered Oct 10 '22 19:10

Tim Perry