java:apache HttpClient >如何禁用重试
我在网站上使用 Apache Httpclient 进行 Ajax 调用。在某些情况下,对外部 Web 服务的请求失败,通常会出现以下情况:
处理请求时捕获 I/O 异常 (java.net.ConnectException):连接超时:连接。
在这种情况下,我通常想跳过重试请求(Httpclient 似乎会自动执行此操作)。
但是,我找不到任何方法、参数等来跳过重试。
有人吗?
谢谢吉尔特·扬
I'm using Apache Httpclient for Ajax-calls on a website. In some cases requests to external webservice fail, often with:
I/O exception (java.net.ConnectException) caught when processing request: Connection timed out: connect.
In that case, more often than not, I want to skip retrying the request (something that Httpclient seems to do automatically) .
However, I can't find any method, param, etc. to skip retrying.
anyone?
Thanks Geert-Jan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从 httpclient 4.3 使用 HttpClientBuilder
From httpclient 4.3 use HttpClientBuilder
这样就可以了。
That would do it.
好的。文档中有问题。 API 和方法也发生了变化。
因此,如果您想使用 DefaultHttpRequestRetryHandler ,可以使用以下方法,
或者
方法中,我们使用具体的 DefaultHttpClient (它是 AbstractHttpClient 的子类,因此具有 setHttpRequestRetryHandler() 方法。)
在第一个 第二个,我们正在对 HttpClient 接口进行编程(遗憾的是,它没有公开该方法,这很奇怪!!呃),所以我们必须进行那个令人讨厌的转换。
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,or
In first one, we use concrete DefaultHttpClient (which is a subclass of AbstractHttpClient and so has the setHttpRequestRetryHandler() method.)
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 教程中有描述。
有关更多信息,请参阅教程,例如这可能是如果请求有副作用(即不是幂等的),则有害。
There's a description in the HttpClient tutorial.
See the tutorial for more information, for instance this may be harmful if the request has side effects (i.e. is not idempotent).
无需强制转换为 AbstractHttpClient。另一种方法是使用 AutoRetryHttpClient 与 DefaultServiceUnavailableRetryStrategy 将重试参数设置为 0。更好的方法是扩展 AbstractHttpClient 或实现 HttpClient 以公开所需的方法。
The cast to AbstractHttpClient is not necessary. Another way is to use a strategy with AutoRetryHttpClient with DefaultServiceUnavailableRetryStrategy set to 0 for retry parameter. A better way would be to extend the AbstractHttpClient or implement HttpClient to expose the desired method.