连接池
java">package cn.lala.crawler.httpclient.test;
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;
@SuppressWarnings("all")
public class HttpClientPoolManagerTest {
public static void main(String[] args) throws IOException {
//创建连接池管理器
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
//设置最大连接数
connectionManager.setMaxTotal(200);
//设置每个主机的最大连接数,指定访问每一个网站的连接数,不会影响其他网站的访问
connectionManager.setDefaultMaxPerRoute(20);
//使用连接池管理器获取连接并发起请求
get(connectionManager);
get(connectionManager);//
}
private static void get(PoolingHttpClientConnectionManager connectionManager) throws IOException {
//使用连接池管理器获取连接
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();
//打印连接,如果两次打印的内存的地址值不一样,证明是不同的连接
//org.apache.http.impl.client.InternalHttpClient@5af3afd9
//org.apache.http.impl.client.InternalHttpClient@33f676f6
System.out.println(httpClient);//
HttpGet httpGet = new HttpGet("https://www.baidu.com");
CloseableHttpResponse response = httpClient.execute(httpGet);
if(response.getStatusLine().getStatusCode()==200){
String html = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println("请求成功,数据的长度是:"+html.length());
}
}
}
设置请求参数
java">package cn.lala.crawler.httpclient.test;
import org.apache.http.client.config.RequestConfig;
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.util.EntityUtils;
import java.io.IOException;
@SuppressWarnings("all")
public class HttpClientRequestConfigTest {
public static void main(String[] args) {
CloseableHttpClient httpClient = HttpClients.createDefault();
//不带参数的get请求
HttpGet httpGet = new HttpGet("https://www.baidu.com/"); //创建连接请求参数
//构建请求配置信息
RequestConfig config = RequestConfig.custom().setConnectTimeout(1000)//创建连接的最长时间
.setConnectionRequestTimeout(500)//从连接池获取连接的最长时间
.setSocketTimeout(10 * 1000)//设置传输数据的最长时间
.build();
//把请求参数设置到httpGet中
httpGet.setConfig(config);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
String html = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println("请求成功,数据的长度是:" + html.length());
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (response != null) {
try { //关闭response
response.close();
//关闭httpClient
httpClient.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}