HttpClient - setting a “global” socket timeout, and a separate timeout per request
-
25-09-2019 - |
Question
With HttpClient, I am setting the default socket/connection timeout with the following:
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(params, 30000);
HttpConnectionParams.setConnectionTimeout(params, 30000);
mClient = new DefaultHttpClient(connectionManager, params);
I'm wondering if I can override these values on a per request basis?
Edit: Would this work?
HttpParams params = req.getParams(); // req is an HttpRequest object
HttpConnectionParams.setSoTimeout(params, 60000);
HttpConnectionParams.setConnectionTimeout(params, 60000);
I tried it, and it seems to, but it's hard to test/create a situation where a timeout will occur.
Solution
If you are using HttpClient 4.0 you could do this :
mClient = new DefaultHttpClient(connectionManager, params) {
protected HttpParams determineParams(HttpRequest req) {
//Fill in your impl here
}
OTHER TIPS
You can simply set those parameters on the request object. For details see: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e391
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow