java: apache HttpClient > how to disable retry

前端 未结 5 2034
再見小時候
再見小時候 2021-02-01 16:41

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

相关标签:
5条回答
  • 2021-02-01 17:04

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

    0 讨论(0)
  • 2021-02-01 17:05
    client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));
    

    That would do it.

    0 讨论(0)
  • 2021-02-01 17:06

    From httpclient 4.3 use HttpClientBuilder

    HttpClientBuilder.create().disableAutomaticRetries().build();
    
    0 讨论(0)
  • 2021-02-01 17:18

    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.

    0 讨论(0)
  • 2021-02-01 17:18

    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.

    0 讨论(0)
提交回复
热议问题