时间:2023-1-17 作者:老大夫 分类: 传智JAVA爬虫学习笔记
这节课的地址不能用了,所以get请求返回的的不是200而是403
package cn.itcast.crawler.test;
import org.apache.hc.client5.http.classic.methods.HttpGet;
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;
import org.apache.hc.core5.net.URIBuilder;
import java.net.URISyntaxException;
public class HttpGetParamTest {
public static void main(String[] args) throws Exception {
//创建HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//设置请求地址是: https://yun.itheima.com/search?keys=Java
//创建URIBuilder
URIBuilder uriBuilder=new URIBuilder("http://yun.itheima.com/search");
//设置参数
uriBuilder.setParameter("keys","Java");
//创建HttpGet对象,url访问地址
HttpGet httpGet = new HttpGet(uriBuilder.build());
System.out.println("这是我们发起请求的信息:"+httpGet);
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();
}
}
//解析响应
}
}
推荐阅读: