31

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

Geert-Jan
  • 18,623
  • 16
  • 75
  • 137

5 Answers5

39

From httpclient 4.3 use HttpClientBuilder

HttpClientBuilder.create().disableAutomaticRetries().build();
Manoj
  • 5,542
  • 9
  • 54
  • 80
32
client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));

That would do it.

Elie
  • 6,915
  • 7
  • 31
  • 35
  • 4
    today this does not compile and one needs to use the docs mention by jdigital => client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); hmmh but it still does not work – Karussell Sep 07 '12 at 22:01
  • 1
    It does not work for me. I tried ((AbstractHttpClient) httpclient).setHttpRequestRetryHandler(myRetryHandler); and it seems to work! – Ali Hashemi May 27 '13 at 19:10
  • doesn't work, maybe because of the use in PoolingClientConnectionManager? – Nativ Jul 09 '13 at 08:40
  • `client.setHttpRequestRetryHandler(..)` works on Android and HttpMethodParams is NOT recognized on Android, due to the version of `httpclient on Android - 4.0-beta1`. – AlikElzin-kilaka Dec 07 '14 at 11:50
6

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,

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.)

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.

Abhishek Tyagi
  • 2,239
  • 1
  • 15
  • 15
5

There's a description in the HttpClient tutorial.

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

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

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
jdigital
  • 11,926
  • 4
  • 34
  • 51
0

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.

Dmitry
  • 89
  • 3