【问题标题】:How to set timeout for each request using Apache httpclient 4.* with PoolingHttpClientConnectionManager?如何使用 Apache httpclient 4.* 和 PoolingHttpClientConnectionManager 为每个请求设置超时?
【发布时间】:2015-03-22 01:15:03
【问题描述】:

如何使用 Apache httpclient 4.3 和 PoolingHttpClientConnectionManager 并为每个请求传递 ConnectTimeOut 和 ReadTimeOut。

例如, 如果我将 CloseableHttpClient 作为 Singleton 并使用 PoolingHttpClientConnection 获取连接,对于我发出的每个请求,我想根据目标主机发送不同的超时值

即。 HostA 10 秒,Host B 5 秒等

请指教。

【问题讨论】:

    标签: java apache-httpclient-4.x connection-timeout


    【解决方案1】:
    HttpGet get1 = new HttpGet("http://hosta/");
    RequestConfig config1 = RequestConfig.custom().setSocketTimeout(10000).build();
    get1.setConfig(config1);
    HttpGet get2 = new HttpGet("http://hostb/");
    RequestConfig config2 = RequestConfig.custom().setSocketTimeout(5000).build();
    get2.setConfig(config2);
    

    【讨论】:

    • 感谢 oleg 赞赏
    猜你喜欢
    • 2020-06-17
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    相关资源
    最近更新 更多