时间:2023-1-17 作者:老大夫 分类: 传智JAVA爬虫学习笔记
注意由于交给连接池管理HttpClient了,所以我们在doGet方法中不要自己关闭HttpClient
package cn.itcast.crawler.test;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import javax.swing.text.html.parser.Entity;
import java.io.IOException;
public class HttpClientPoolTest {
public static void main(String[] args) {
//创建连接池管理器
PoolingHttpClientConnectionManager cm=new PoolingHttpClientConnectionManager();
//设置最大连接数
cm.setMaxTotal(100);
//设置每个主机的最大连接数
cm.setDefaultMaxPerRoute(10);
//使用连接池管理器发起请求
doGet(cm);
//doPost(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.getCode()==200){
String content = EntityUtils.toString(response.getEntity(), "utf8");
System.out.println(content.length());
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(response!=null){
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//不能关闭httpclient,由连接池管理httpclient
//httpClient.close();
}
}
}
推荐阅读: