Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit timeout in HttpClient (4.5)

I need to be able to modify the timeout set in my CloseAbleHttpClient. Here's how I set the different timeouts:

RequestConfig config = RequestConfig.copy(RequestConfig.DEFAULT)
            .setConnectTimeout(timeout)
            .setSocketTimeout(timeout)
            .setConnectionRequestTimeout(managerTimeout)
            .build();

    httpClient = HttpClients.custom()
            .setConnectionManager(connectionManager)
            .setDefaultRequestConfig(config)
            .build();

Do I have to create a new RequestConfig object, and build the HttpClients.custom() again to achieve this? I have tried this, and the problem arises in that I need my CloseableHttpClient to be final. If I set the timeout 2 times, this isn't possible. I would appreciate any input on how to best modify the timout properties!

like image 294
user16655 Avatar asked Nov 01 '25 22:11

user16655


1 Answers

There is nothing that stops your from using different request config on a per request basis

RequestConfig defaultRequestConfig = RequestConfig.custom()
        .setConnectTimeout(timeout)
        .setSocketTimeout(timeout)
        .setConnectionRequestTimeout(managerTimeout)
        .build();

CloseableHttpClient httpClient = HttpClients.custom()
        .setConnectionManager(connectionManager)
        .setDefaultRequestConfig(defaultRequestConfig)
        .build();

RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig)
        .setConnectTimeout(timeout * 2)
        .setSocketTimeout(timeout * 2)
        .setConnectionRequestTimeout(managerTimeout * 2)
        .build();

HttpGet get = new HttpGet();
get.setConfig(requestConfig);
like image 184
ok2c Avatar answered Nov 03 '25 13:11

ok2c



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!