HttpClient executes requests multiple time if request timed out

牧云@^-^@ 提交于 2021-02-07 03:14:41

问题


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?


回答1:


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.




回答2:


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

HttpClientBuilder.create().disableAutomaticRetries().build();



回答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



来源:https://stackoverflow.com/questions/23054289/httpclient-executes-requests-multiple-time-if-request-timed-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!