准备工作
导入依赖
XML
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
yml
XML
logging:
level:
root: info
com.lrm: debug
1.入门程序(获取到静态页面)
java
package com.itheima.reggie.utils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
/**
* @Author lpc
**/
public class CrawlerFirst {
public static void main(String[] args) throws Exception {
//1.打开浏览器,创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//2.输入网址,发起get请求创建HttpGet对象
HttpGet httpGet = new HttpGet("http://www.itcast.cn");
//3.按回车,发起请求,返回响应,使用Httpclient对象发起请求
CloseableHttpResponse response = httpClient.execute(httpGet);
//4.解析响应,获取数据
//判斯状态码是否是200
if (response.getStatusLine().getStatusCode()==200){
HttpEntity httpEntity = response.getEntity();
//获取前端静态页面
String content = EntityUtils.toString(httpEntity,"utf8");
System.out.println(content);
}
}
}
2.HttpClient---Get
java
package com.itheima.reggie.utils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
/**
* @Author lpc
* @Date 2024 03 12 00 23
**/
public class CrawlerFirst {
public static void main(String[] args){
//1.打开浏览器,创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//2.输入网址,发起get请求创建HttpGet对象
HttpGet httpGet = new HttpGet("http://www.itcast.cn");
//3.按回车,发起请求,返回响应,使用Httpclient对象发起请求
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
//4.解析响应,获取数据
//判斯状态码是否是200
if (response.getStatusLine().getStatusCode()==200){
HttpEntity httpEntity = response.getEntity();
//获取前端静态页面
String content = EntityUtils.toString(httpEntity,"utf8");
System.out.println(content.length());
}
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
//关闭response
response.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
//关闭浏览器
httpClient.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}