HttpClient executes requests multiple time if request timed out
HttpClient executes request 4 times if it times out. If it does not time out then it is working fine. Is it related to HttpClient?
I found that it is HttpClient's default behaviour to execute requests 4 times if it fails. I am not sure about other kind of failures but at least with time out.
To disable this behaviour do this :
DefaultHttpClient client = new DefaultHttpClient(); // Disable default behavior of HttpClient of retrying requests in case of failure ((AbstractHttpClient) client).setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
Here retry count is set to 0 to disable retry.
I found solution from this blog.
This resolved the issue for me. Using httpclient 4.3 and above.
Apache HttpClient tries to connect 5 times in case of transport exception. Here is what doc says:
HttpClient will automatically retry up to 5 times those methods that fail with a transport exception while the HTTP request is still being transmitted to the target server (i.e. the request has not been fully transmitted to the server).
To change this behaviour you need to implement HttpMethodRetryHandler interface