Search code examples
javaandroidhttpclientapache-httpclient-4.xapache-commons-httpclient

Should I need to specify a timeout when using Apache's HttpClient?


The default values are infinite.

But as I am using Apache Client [1] in Android, since mobile network is always unreliable, so should I need to set the http.socket.timeout and http.connection.timeout?

If yes, what value I should set? Is one minute a suitable value for mobile network (e.g. 3G)?

[1] http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html


Solution

  • If you cannot use the AndroidHttpClient you can always set the same timeout values, namely:

    // Default connection and socket timeout of 60 seconds.  Tweak to taste.
    private static final int SOCKET_OPERATION_TIMEOUT = 60 * 1000;
    

    This constant is used for a lot of timeouts,

    HttpConnectionParams.setConnectionTimeout(params, SOCKET_OPERATION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SOCKET_OPERATION_TIMEOUT);
    

    and also used when calling SSLCertificateSocketFactory.getHttpSocketFactory(..)