码迷,mamicode.com
首页 > 编程语言 > 详细

20200726_java爬虫_使用HttpClient模拟浏览器发送请求

时间:2020-07-26 15:44:20      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:size   技术   utf-8   浏览器   div   depend   ati   enc   created   

0. 摘要

0.1 添加依赖

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>

 

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 添加依赖

技术图片

 

 1.1.2 依赖代码

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>

 

1.2 添加 TestHttpClient 类

1.2.1 创建类文件 com.aifu.TestHttpClient

技术图片

 

 

1.2.2 添加代码

技术图片

 

 

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

技术图片

 

20200726_java爬虫_使用HttpClient模拟浏览器发送请求

标签:size   技术   utf-8   浏览器   div   depend   ati   enc   created   

原文地址:https://www.cnblogs.com/aifu/p/study_java_crawler_010_TestHttpClient.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!