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?
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.
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.
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.
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.
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();
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With