HttpClient executes requests multiple time if request timed out
Asked Answered
F

3

6

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?

Fluoresce answered 14/4, 2014 at 7:25 Comment(2)
Where is code, that execute HttpClient four times !!!Pentamerous
@Kedarnath I knew the answer and issue was not in my code. So I did not add code. I have posted answer already.Fluoresce
F
8

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.

Fluoresce answered 14/4, 2014 at 7:27 Comment(0)
I
4

This resolved the issue for me. Using httpclient 4.3 and above.

HttpClientBuilder.create().disableAutomaticRetries().build();
Indocile answered 23/5, 2018 at 3:55 Comment(2)
@AliHashemi That should work. Would you mind sharing some code sample or provide more context on how you're trying to use this. it's well documented here hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/…Indocile
Yes it works. Sorry I forgot to come back and delete this comment. The problem was something else.Disruptive
M
3

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

Maintenance answered 14/4, 2014 at 7:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.