HttpClient 4.3.5 使用总结

本文详细介绍了如何使用Apache HttpClient进行Http请求,包括创建CloseableHttpClient、设置超时、配置连接池、处理请求参数及响应结果。强调了在线上环境中对超时和连接池的合理配置以避免阻塞问题,提供了示例代码以供参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、前言

调用HTTP接口时经常会使用Apache HttpClient进行Http请求发送,平常只是简单的使用,今天对具体的使用进行了一个总结,方便以后查询,因为Apache HttpClient每次版本升级API变动都很大,这里使用的是以较新的版本:4.5.3

二、使用方法

1、创建CloseableHttpClient

首先需要创建一个CloseableHttpClient,然后通过CloseableHttpClient进行get、post、put等请求方式的调用

//官网的示例
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://targethost/homepage");
CloseableHttpResponse response = httpclient.execute(httpGet);
try {
    System.out.println(response.getStatusLine());
    HttpEntity entity = response.getEntity();
    // do something useful with the response body
    // and ensure it is fully consumed
    EntityUtils.consume(entity);
} finally {
    response.close();
}

使用方法相对简单,但是默认情况下各种超时设置都为-1,即无限制,这样会导致各种超时阻塞问题的发生,因此在线上的使用过程中应该对一些常见的超时设置进行自定义。

2、超时设置

Appache HttpClient提供了一个配置类,用于请求的超时等其他配置

//HttpClient请求超时配置
RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(CONNECT_TIMEOUT)
                    .setSocketTimeout(SOCKET_TIMEOUT)
                    .setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)
                    .build();

源码中的3个超时设置:
connectionRequestTimeout :从连接池中获取可用连接的时间
connectTimeout :连接超时时间,客户端请求服务器与服务器建立连接(三次握手)成功的最大接受时间
socketTimeout :请求获取数据的超时时间,访问一个接口指定时间内无法返回数据,直接放弃此次调用

Builder() {
            super();
            this.staleConnectionCheckEnabled = false;
            this.redirectsEnabled = true;
            this.maxRedirects = 50;
            this.relativeRedirectsAllowed = true;
            this.authenticationEnabled = true;
            this.connectionRequestTimeout = -1;
            this.connectTimeout = -1;
            this.socketTimeout = -1;
            this.contentCompressionEnabled = true;
        }

可以看到源码中默认赋值为-1,这里可以通过上述RequestConfig 进行配置。

3、连接池配置

建立一次连接是需要耗费一定时间与资源的,常见的方式是通过连接池来提高效率,Appache HttpClient也提供了相关的连接池配置:

PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(POOL_MAX_TOTAL);
connectionManager.setDefaultMaxPerRoute(POOL_MAX_PRE_ROUTE);
  • setMaxTotal()方法用来设置连接池的最大连接数,即整个池子的大小;
  • setDefaultMaxPerRoute()方法来设置每一个路由的最大连接数,这里的路由是指IP+PORT,例如连接池大小(MaxTotal)设置为300,路由连接数设置为200(DefaultMaxPerRoute),对于www.a.comwww.b.com两个路由来说,发起服务的主机连接到每个路由的最大连接数(并发数)不能超过200,两个路由的总连接数不能超过300。

因此,最终我们可以这样来获取CloseableHttpClient:

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

4、请求参数设置

实际情况中我们请求接口服务时参数比较多,可以通过HttpClient提供的方法来方便的构建请求参数:

//get参数拼接,url为请求接口地址
URIBuilder builder = new URIBuilder(url);
//通过setParameter来设置参数key-value
builder.setParameter("params-1", "value-1");
builder.setParameter("params-2", "value-2");
HttpGet httpGet = new HttpGet(builder.build());

//post方式参数构造
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> values = new ArrayList<NameValuePair>();
values.add(new BasicNameValuePair("params-1", "value-1"));
values.add(new BasicNameValuePair("params-2", "value-2"));
httpPost.setEntity(new UrlEncodedFormEntity(values, "UTF-8"));

5、返回结果处理

//这里以post为例
CloseableHttpResponse response = httpclient.execute(httpPost)
//消费服务器响应内容
EntityUtils.toString(response.getEntity(), "UTF-8");

6、连接释放与关闭

//连接池关闭
CloseableHttpClient .close();
//下面两个方法均可以释放连接到连接池
EntityUtils.toString(response.getEntity(), "UTF-8");
EntityUtils.consume(response.getEntity());

这里需要注意的时,在使用连接池的情况下要慎用CloseableHttpClient .close(),因为该方法会关闭整个连接池,源码如下:

List<Closeable> closeablesCopy = closeables != null ? new ArrayList<Closeable>(closeables) : null;
        if (!this.connManagerShared) {
            if (closeablesCopy == null) {
                closeablesCopy = new ArrayList<Closeable>(1);
            }
            //连接池管理类
            final HttpClientConnectionManager cm = connManagerCopy;
            if (evictExpiredConnections || evictIdleConnections) {
                final IdleConnectionEvictor connectionEvictor = new IdleConnectionEvictor(cm,
                        maxIdleTime > 0 ? maxIdleTime : 10, maxIdleTimeUnit != null ? maxIdleTimeUnit : TimeUnit.SECONDS);
                closeablesCopy.add(new Closeable() {
                    @Override
                    public void close() throws IOException {
                        connectionEvictor.shutdown();
                    }
                });
                connectionEvictor.start();
            }
            closeablesCopy.add(new Closeable() {
                @Override
                public void close() throws IOException {
                    //关闭连接池
                    cm.shutdown();
                }

            });
        }
        //最终使用的是InternalHttpClient执行execute()方法
        return new InternalHttpClient(
                execChain,
                connManagerCopy,
                routePlannerCopy,
                cookieSpecRegistryCopy,
                authSchemeRegistryCopy,
                defaultCookieStore,
                defaultCredentialsProvider,
                defaultRequestConfig != null ? defaultRequestConfig : RequestConfig.DEFAULT,
                closeablesCopy);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值