Apache HttpClient 4.3 - setting connection idle timeout

后端 未结 2 447
没有蜡笔的小新
没有蜡笔的小新 2021-02-07 08:24

What\'s the shortest way to configure connection idle timeout on Apache HttpClient 4.3 version?

I\'ve looked in the documentation and couldn\'t find anything. My goal is

2条回答
  •  广开言路
    2021-02-07 09:04

    The timeout is set in the RequestConfig so you could set the default when the HttpClientBuilder is called.

    For example assuming your timeout variable is in seconds to create your custom RequestConfig you could do something like this:

    RequestConfig config = RequestConfig.custom()
        .setSocketTimeout(timeout * 1000)
        .setConnectTimeout(timeout * 1000)
        .build();
    

    You could then build your HttpClient setting the default RequestConfig like this:

    HttpClients.custom()
        .setDefaultRequestConfig(config);
    

提交回复
热议问题