HttpClient 4.5.2版本设置连接超时时间-CloseableHttpClient设置Timeout

HttpClient  4.5版本设置连接超时时间-CloseableHttpClient设置Timeout(区别于4.3.2)

HttpClient升级到4.5版本后,API有很多变化,HttpClient 4之后,API一直没有太稳定,我感觉4.5版本抽象后,很多API应该快稳定了。

       使用HttpClient,一般都需要设置连接超时时间和获取数据超时时间。这两个参数很重要,目的是为了防止访问其他http时,由于超时导致自己的应用受影响。

4.5版本中,这两个参数的设置都抽象到了RequestConfig中,由相应的Builder构建,具体的例子如下:

  1. CloseableHttpClient httpclient = HttpClients.createDefault();  

  2. HttpGet httpGet = new HttpGet("http://stackoverflow.com/");  

  3. RequestConfig requestConfig = RequestConfig.custom()  

  4.         .setConnectTimeout(5000).setConnectionRequestTimeout(1000)  

  5.         .setSocketTimeout(5000).build();  

  6. httpGet.setConfig(requestConfig);  

  7. CloseableHttpResponse response = httpclient.execute(httpGet);  

  8. System.out.println("得到的结果:" + response.getStatusLine());//得到请求结果  

  9. HttpEntity entity = response.getEntity();//得到请求回来的数据

setConnectTimeout:设置连接超时时间,单位毫秒。

setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。

setSocketTimeout:请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。

===========================================

昨天遇到一个问题需要设置CloseableHttpClient的超时时间,查了官方文档如下。

新建一个RequestConfig:

  1. RequestConfig defaultRequestConfig = RequestConfig.custom()

  2. .setSocketTimeout(5000)

  3. .setConnectTimeout(5000)

  4. .setConnectionRequestTimeout(5000)

  5. .setStaleConnectionCheckEnabled(true)

  6. .build();

这个超时可以设置为客户端级别,作为所有请求的默认值:

  1. CloseableHttpClient httpclient = HttpClients.custom()

  2. .setDefaultRequestConfig(defaultRequestConfig)

  3. .build();

Request不会继承客户端级别的请求配置,所以在自定义Request的时候,需要将客户端的默认配置拷贝过去:

  1. HttpGet httpget = new HttpGet("http://www.apache.org/");

  2. RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig)

  3. .setProxy(new HttpHost("myotherproxy", 8080))

  4. .build();

  5. httpget.setConfig(requestConfig);

 伦理片 http://www.dotdy.com/  

4.3版本的超时是这样的:

public static String httpPost(String url, String jsonString) {
    // 设置HTTP请求参数
String result = null;
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    try {
        httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);//设置请求超时时间 10s
StringEntity entity = new StringEntity(jsonString);
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        HttpEntity resEntity = httpClient.execute(httpPost).getEntity();
        result = EntityUtils.toString(resEntity, "UTF-8");
    } catch (Exception e) {
        logger.error("http接口调用异常:url is::" + url, e);
        return null;
    } finally {
        httpClient.getConnectionManager().shutdown();
    }
    return result;
}

 

4.5.2版本是这样的:

 public static String testTimeout(String url) {

        // 设置HTTP请求参数

        String result = null;

        CloseableHttpClient client = HttpClients.createDefault();

        HttpGet httpGet = new HttpGet(url);

        RequestConfig requestConfig = RequestConfig.custom()

                .setConnectTimeout(50000).setConnectionRequestTimeout(10000)

                .setSocketTimeout(50000).build();

        httpGet.setConfig(requestConfig);

        try {

            CloseableHttpResponse response = client.execute(httpGet);

            result = EntityUtils.toString(response.getEntity(), "UTF-8");

        } catch (ClientProtocolException e) {

            logger.error("http接口调用异常:url is::" + url, e);

            return null;

        } catch (Exception e) {

            logger.error("http接口调用异常:url is::" + url, e);

            return null;

        } finally {

            try {

                client.close();

            } catch (IOException e) {

                logger.error("http接口调用异常:url is::" + url, e);

            }

        }

        return result;

    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值