简介:HttpClient是一个HTTP客户端编程工具,用于获取网页数据
添加依赖
在网页 https://mvnrepository.com/ 查找自已所需要的依赖
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
GET请求:
步骤:
- 创建 HttpClient 的实例
- 创建HttpClient对象,设置url访问地址
- 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例
- 读 response
- 释放连接。无论执行方法是否成功,都必须释放连接
- 对得到后的内容进行处理
//1.创建HttpClient对象 相当于浏览器
CloseableHttpClient httpClient = HttpClients.createDefault();
//2.创建HttpGet对象,设置url访问地址
HttpGet httpGet = new HttpGet("http://www.baidu.com/");
//响应模型
CloseableHttpResponse response = null;
try {
//3.使用httpClient的 xecute方法执行第二步骤创建的实例来发起get请求,获取response
response = httpClient.execute(httpGet);
System.out.println("响应状态:" + response.getStatusLine());
//4.读response
//200代表成功访问
if(response.getStatusLine().getStatusCode() == 200){
//响应内容
String content = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println("---------------------------");
System.out.println(content);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//5.释放连接
try {
response.close();
httpClient.close();
}catch (IOException e){
e.printStackTrace();
}
}
Post请求
Post请求与不带参数的get请求类似,只需要将HttpGet改为HttpPost即可
HTTP中Get和Post两种基本请求方法的区别:
请查看:https://www.cnblogs.com/logsharing/p/8448446.html
连接池
每次请求都需要创建HttpClient,会有频繁创建和销毁的问题,(相当于每次访问都打开一次浏览器,使用完再关闭浏览器),因此可以使用连接池来解决资源浪费、影响性能的问题.
/创建连接池管理器
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
//设置连接数
poolingHttpClientConnectionManager.setMaxTotal(20);
//设置每个主机的最大连接数(爬取可能不是一个主机的数据,分配连接使均衡一些,类似于爬取百度分配5个,爬取新浪5个...)
//主机:相当于一个Host
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(5);
//使用连接池管理器发送请求
get(poolingHttpClientConnectionManager);
get(poolingHttpClientConnectionManager);
get方法中主要变化在第一步和第五步发生变化
public static void get(PoolingHttpClientConnectionManager poolingHttpClientConnectionManager){
//1.不是每次创建新的HttpClient,而是从连接池中获取HttpClient对象
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(poolingHttpClientConnectionManager).build();
//2.创建HttpGet对象,设置url访问地址
HttpGet httpGet = new HttpGet("http://www.baidu.com/");
//响应模型
CloseableHttpResponse response = null;
try {
//3.使用httpClient的 xecute方法执行第二步骤创建的实例来发起get请求,获取response
response = httpClient.execute(httpGet);
System.out.println("响应状态:" + response.getStatusLine());
//4.读response
//200代表成功访问
if(response.getStatusLine().getStatusCode() == 200){
//响应内容
String content = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println("---------------------------");
System.out.println(content);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//5.释放连接
try {
response.close();
//不能关闭HttpClient,由连接池管理
//httpClient.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
请求参数配置
有时候由于网络,或者是目标服务器的原因,请求需要更长的时间才能完成,因此我们需要设置请求参数来自定义相关时间.
// 设置请求信息
private RequestConfig getConfig() {
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(1000) // 创建连接的最长时间,ms
.setConnectionRequestTimeout(500) // 获取连接的最长时间,ms
.setSocketTimeout(10000) // 数据传输的最长时间,ms
.build();
return config;
}
在get请求中添加一行代码调用该方法即可
// 创建httpGet请求对象, 设置url地址
HttpGet httpGet = new HttpGet(url);
// 设置请求信息
httpGet.setConfig(getConfig());