时间:2023-1-17 作者:老大夫 分类: 传智JAVA爬虫学习笔记
package cn.itcast.crawler.test;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.io.entity.EntityUtils;
public class HttpGetTest {
public static void main(String[] args) {
//创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建HttpGet对象,url访问地址
org.apache.hc.client5.http.classic.methods.HttpGet httpGet = new org.apache.hc.client5.http.classic.methods.HttpGet("http://www.itcast.cn");
CloseableHttpResponse response = null;
try {
//使用HttpClient发起请求,获取response
response = httpClient.execute(httpGet);
//解析响应
if(response.getCode()==200){
String content = EntityUtils.toString(response.getEntity(), "utf8");
System.out.println(content.length());
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//关闭response
try {
response.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//解析响应
}
}
推荐阅读:
02 入门程序
22 封装HttpClient
24 实现爬虫功能2
20 开发准备
18 selector选择器的组合使用获取元素
23 实现爬虫功能1
17 使用选择器获取元素
21 编写案例的引导类,Service,Dao,Pojo
19 idea工程文件打包
15 使用DOM的方式获取文档
行动消除疑虑