Java爬虫HTTPClient -- 连接池

如果每次请求都要创建HTTPClient,会有频繁创建和销毁的问题,可以使用连接池来解决这个问题。

测试一下代码,并断点查看每次获取的HTTPClient都是不一样的。

直接上代码

package xxx.xxx.xxx;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.sql.SQLOutput;

public class HttpClientPoolTest {
    public static void main(String[] args) {
        //创建连接池管理器
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

        //设置最大连接数
        cm.setMaxTotal(100);

        //设置每个主机的最大连接数
        cm.setDefaultMaxPerRoute(10);

        //使用连接池管理器发送请求
        doGet(cm);
    }

    private static void doGet(PoolingHttpClientConnectionManager cm)  {
        //不是每次创建新的HTTPClient,而是从连接池汇总获取HTTPClient对象
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();

        HttpGet httpGet = new HttpGet("http:///www.itcast.cn");
        CloseableHttpResponse response =null;
        try{
            response= httpClient.execute(httpGet);

            if(response.getStatusLine().getStatusCode() == 200){
                String content = EntityUtils.toString(response.getEntity(), "utf8");
                System.out.println(content.length());
            }
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            if(response != null){
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
               //不能关闭httpclientclose      即 httpClient.close();
            }
        }
    }
}
发布了81 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43542074/article/details/103107321