java爬虫模拟post请求_java爬虫之使用HttpClient模拟浏览器发送请求方法详解

0. 摘要

0.1 添加依赖

org.apache.httpcomponents

httpclient

4.5.2

0.2 代码

//1. 打开浏览器 创建httpclient对象

CloseableHttpClient httpClient = HttpClients.createDefault();

//2. 输入网址

HttpGet httpGet = new HttpGet("http://www.baidu.com");

//3. 发送请求

CloseableHttpResponse httpResponse = httpClient.execute(httpGet);

//4. 响应结果

HttpEntity httpEntity = httpResponse.getEntity();

//5. 解析结果

String result = EntityUtils.toString(httpEntity, "utf-8");

System.out.println(result);

1. 实操

1.1 添加依赖

1.1.1 找到 pom.xml 添加依赖

3dc79ab06cad9d95e40cafa240906cd8.png

1.1.2 依赖代码

org.apache.httpcomponents

httpclient

4.5.2

1.2 添加 TestHttpClient 类

1.2.1 创建类文件 com.aifu.TestHttpClient

ab0d44bff6cedc98757c2cd0e152861e.png

1.2.2 添加代码

438437ebfc859a109a5acc39b5493d81.png

public static void main(String[] args) throws IOException {

//1. 打开浏览器 创建httpclient对象

CloseableHttpClient httpClient = HttpClients.createDefault();

//2. 输入网址

HttpGet httpGet = new HttpGet("http://www.baidu.com");

//3. 发送请求

CloseableHttpResponse httpResponse = httpClient.execute(httpGet);

//4. 响应结果

HttpEntity httpEntity = httpResponse.getEntity();

//5. 解析结果

String result = EntityUtils.toString(httpEntity, "utf-8");

System.out.println(result);

}

1.3 运行

1.3.1 点击绿标运行 或者快捷键 ctrl + alt +F10

dee5414b7a06071ef0d794b6bcf8582b.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值