java: apache HttpClient > 如何禁用重试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2285799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:40:14  来源:igfitidea点击:

java: apache HttpClient > how to disable retry

javahttpclient

提问by Geert-Jan

I'm using Apache Httpclient for Ajax-calls on a website. In some cases requests to external webservice fail, often with:

我在网站上使用 Apache Httpclient 进行 Ajax 调用。在某些情况下,对外部 Web 服务的请求会失败,通常是:

I/O exception (java.net.ConnectException) caught when processing request: Connection timed out: connect.

处理请求时捕获的 I/O 异常 (java.net.ConnectException):连接超时:连接。

In that case, more often than not, I want to skip retrying the request (something that Httpclient seems to do automatically) .

在这种情况下,通常情况下,我想跳过重试请求(Httpclient 似乎自动执行的操作)。

However, I can't find any method, param, etc. to skip retrying.

但是,我找不到任何方法、参数等来跳过重试。

anyone?

任何人?

Thanks Geert-Jan

感谢 Geert-Jan

采纳答案by Elie

client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));

That would do it.

这样就可以了。

回答by jdigital

There's a description in the HttpClient tutorial.

HttpClient 教程中有说明。

 client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
           new DefaultHttpMethodRetryHandler());

See the tutorialfor more information, for instance this may be harmful if the request has side effects (i.e. is not idempotent).

有关更多信息,请参阅教程,例如,如果请求有副作用(即不是幂等的),这可能是有害的。

回答by Abhishek Tyagi

OK. There is issue in the Documentation. Also there has been change in API and methods. So if you want to use DefaultHttpRequestRetryHandler, here are the ways to do that,

好的。文档中有问题。API和方法也发生了变化。所以如果你想使用DefaultHttpRequestRetryHandler,这里是这样做的方法,

DefaultHttpClient httpClient = new DefaultHttpClient();
DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(0, false);
httpClient.setHttpRequestRetryHandler(retryHandler);

or

或者

HttpClient httpClient = new DefaultHttpClient();
DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(0, false);
((AbstractHttpClient)httpClient).setHttpRequestRetryHandler(retryHandler);

In first one, we use concrete DefaultHttpClient (which is a subclass of AbstractHttpClient and so has the setHttpRequestRetryHandler() method.)

在第一个中,我们使用具体的 DefaultHttpClient(它是 AbstractHttpClient 的子类,因此具有 setHttpRequestRetryHandler() 方法。)

In second one, we are programming to the HttpClient interface (which sadly doesn't expose that method, and this is weird !! ehh), so we have to do that nasty cast.

在第二个中,我们正在对 HttpClient 接口进行编程(遗憾的是它没有公开那个方法,这很奇怪!!呃),所以我们必须做那个讨厌的演员。

回答by Dmitry

The cast to AbstractHttpClient is not necessary. Another way is to use a strategy with AutoRetryHttpClientwith DefaultServiceUnavailableRetryStrategyset to 0 for retry parameter. A better way would be to extend the AbstractHttpClient or implement HttpClient to expose the desired method.

不需要对 AbstractHttpClient 进行强制转换。另一种方法是使用一种战略,AutoRetryHttpClientDefaultServiceUnavailableRetryStrategy设置为0的重试参数。更好的方法是扩展 AbstractHttpClient 或实现 HttpClient 以公开所需的方法。

回答by Manoj

From httpclient 4.3 use HttpClientBuilder

从 httpclient 4.3 开始使用 HttpClientBuilder

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