
java爬虫.HttpClient.Get请求
发布日期:2021-05-07 02:46:03
浏览次数:24
分类:精选文章
本文共 4580 字,大约阅读时间需要 15 分钟。
Get请求
HttpGet请求响应的一般步骤:
1). 创建HttpClient对象,可以使用HttpClients.createDefault(); 2). 如果是无参数的GET请求: 直接使用构造方法HttpGet(String url)创建HttpGet对象即可; 如果是带参数GET请求: 先使用URIBuilder(String url)创建对象, 再调用addParameter(String param, String value),或setParameter(String param, String value)来设置请求参数, 并调用build()方法构建一个URI对象。只有构造方法HttpGet(URI uri)来创建HttpGet对象。
3). 创建HttpResponse:
调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头; 调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。 程序可通过该对象获取服务器的响应内容,通过调用getStatusLine().getStatusCode()可以获取响应状态码。 4). 释放连接。无参
例:
先从maven资源站上提取httpclient相关源码,编辑.xml文件。
4.0.0 cn.itcast itcast-crawler-first pom 1.0-SNAPSHOT org.apache.httpcomponents httpclient 4.5.2 org.slf4j slf4j-log4j12 1.7.25
再创建个.Java测试类
package cn.csdn.crawlar.test;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;public class HttpGetTest { public static void main(String[] args) { //创建HttpClient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); //创建HttpGet对象,设置url访问地址 String uri = "https://www.csdn.net/"; HttpGet httpGet = new HttpGet(uri); //try/catch/finally : Ctrl+Alt+T CloseableHttpResponse response = null; try { //使用HttpClient发起请求,获取response response = httpClient.execute(httpGet); //解析响应 if (response.getStatusLine().getStatusCode() == 200) { String content = EntityUtils.toString(response.getEntity(), "utf8"); System.out.println(content.length()); } } catch (IOException e) { e.printStackTrace(); } finally { //关闭response try { response.close(); } catch (IOException e) { e.printStackTrace(); } try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } }}
在运行后的控制面上,将会找出如图所示的一些内容: 首行是Get请求,接着会发现下面有 HTTP/1.1 200 OK[\r][\n] ,200则表示请求成功。

含参
package cn.csdn.crawlar.test;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.utils.URIBuilder;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import java.io.IOException;public class HttpGetTest含参 { public static void main(String[] args) throws Exception { //创建HttpClient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); //设置请求地址是:https://www.icourse163.org/search.htm?search=java#/ //创建URI地址 String string="https://www.icourse163.org/search.htm"; URIBuilder uriBuilder = new URIBuilder( string ); //设置参数 String param="search",value="java"; uriBuilder.setParameter(param,value ); //创建HttpGet对象,设置url访问地址 HttpGet httpGet = new HttpGet(uriBuilder.build()); //发起请求 System.out.println("发起请求的信息"+httpGet); //try/catch/finally : Ctrl+Alt+T CloseableHttpResponse response = null; try { //使用HttpClient发起请求,获取response response = httpClient.execute(httpGet); //解析响应 if (response.getStatusLine().getStatusCode() == 200) { String content = EntityUtils.toString(response.getEntity(), "utf8"); System.out.println(content.length()); } } catch (IOException e) { e.printStackTrace(); } finally { //关闭response try { response.close(); } catch (IOException e) { e.printStackTrace(); } try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } }}
结果也会显示出有一些基础的设置,以及访问网站的相关链接

发表评论
最新留言
表示我来过!
[***.240.166.169]2025年04月19日 07时30分15秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
pytest封神之路第二步 132个命令行参数用法
2021-05-09
字符集其实很简单
2021-05-09
Django认证系统并不鸡肋反而很重要
2021-05-09
快用Django REST framework写写API吧
2021-05-09
tep用户手册帮你从unittest过渡到pytest
2021-05-09
12张图打开JMeter体系结构全局视角
2021-05-09