java爬虫 构造http请求_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 添加依赖

dd42632fc9640fc958f46293b9ddd749.png

1.1.2 依赖代码

org.apache.httpcomponents

httpclient

4.5.2

1.2 添加 TestHttpClient 类

1.2.1 创建类文件 com.aifu.TestHttpClient

b24c4d8baed38d027d454a78e74563e6.png

1.2.2 添加代码

72bd470b2cb7e111f75c29628b1bcfb7.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

7d8b728e1e7fd51dbc03d3f88add4330.png

到此这篇关于java爬虫之使用HttpClient模拟浏览器发送请求方法详解的文章就介绍到这了,更多相关java爬虫HttpClient模拟浏览器发送请求内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

时间: 2020-07-25

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值