HttpClient 4 必须设超时时间,否则线程一直等在那.

      

  // Note: must set timeout,otherwise alway wait here.

        // set time out

        // conntction timeout 20s

        httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);

        // transfer time 60s

        httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!要解决HttpClient的连接超时问题,您可以尝试以下方法: 1. 置连接超时时间: 使用HttpClient的`RequestConfig`类来置连接超时时间。可以通过`setConnectTimeout`方法置连接超时时间(以毫秒为单位)。 ```java RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(5000) // 置连接超时时间为5秒 .build(); HttpGet httpGet = new HttpGet("http://example.com"); httpGet.setConfig(requestConfig); ``` 2. 置读取超时时间: 同样地,您也可以使用`setSocketTimeout`方法来置读取超时时间。这个超时时间表示从服务器获取数据的最大等待时间(以毫秒为单位)。 ```java RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(5000) // 置读取超时时间为5秒 .build(); HttpGet httpGet = new HttpGet("http://example.com");httpGet.setConfig(requestConfig); ``` 3. 置请求连接管理器: 如果您使用了连接池或者多线程HttpClient实例,您可能需要置请求连接管理器的连接超时时间。 ```java ConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); ((PoolingHttpClientConnectionManager) connectionManager).setConnectTimeout(5000); // 置连接超时时间为5秒 CloseableHttpClient httpClient = HttpClientBuilder.create() .setConnectionManager(connectionManager) .build(); HttpGet httpGet = new HttpGet("http://example.com"); ``` 请根据您的具体需求选择适合的方法进行配置,希望对您有所帮助!如有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值