【Java爬虫】HttpClient样例
2022/3/21 11:58:39
本文主要是介绍【Java爬虫】HttpClient样例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
HttpClient简介
HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议。HttpClient已经应用在很多的项目中。
下载地址:https://hc.apache.org/httpclient-legacy/
不过一般在项目中是直接调用pom依赖
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency>
一般使用步骤
使用HttpClient发送请求和接收响应一般有以下步骤:
- 创建HttpClient对象。
- 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
- 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HttpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。
- 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
- 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
- 释放连接。无论执行方法是否成功,都必须释放连接
发送Get请求
public void httpClientTest() throws Exception{ CloseableHttpClient httpClient= HttpClients.createDefault(); //不带参数 //HttpGet get=new HttpGet("http://www.it.cn"); //带参数 HttpGet get=new HttpGet("http://yun.com/course/936.html?capid=1$czpc$jingjiaczpz-PC-1"); CloseableHttpResponse response=httpClient.execute(get); StatusLine statusLine=response.getStatusLine(); System.out.println(statusLine); int statusCode=statusLine.getStatusCode(); System.out.println(statusCode); HttpEntity entity= response.getEntity(); String html= EntityUtils.toString(entity,"utf-8"); System.out.println(html); response.close(); httpClient.close(); }
发送Post请求
public void testPost() throws Exception{ CloseableHttpClient httpClient=HttpClients.createDefault(); HttpPost post=new HttpPost("http://yun.com/course/936.html"); //创建list存放请求表单 List<NameValuePair>form=new ArrayList<>(); //较多的提交数据可以增元素 form.add(new BasicNameValuePair("capid","1$czpc$jingjiaczpz-PC-1")); // form.add(new BasicNameValuePair("capid","1$czpc$jingjiaczpz-PC-1")); //form.add(new BasicNameValuePair("capid","1$czpc$jingjiaczpz-PC-1")); HttpEntity entity=new UrlEncodedFormEntity(form); post.setEntity(entity); CloseableHttpResponse response= httpClient.execute(post); String s = EntityUtils.toString(response.getEntity(), "utf-8"); System.out.println(s); response.close(); httpClient.close(); }
请求连接池
public void testPoolingConnectManager() throws Exception{ PoolingHttpClientConnectionManager cm=new PoolingHttpClientConnectionManager(); CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build(); HttpGet get=new HttpGet("http://www.it.cn"); CloseableHttpResponse execute = httpClient.execute(get); String s = EntityUtils.toString(execute.getEntity(), "utf-8"); System.out.println(s); execute.close(); //此处不需要关闭连接池 会直接被连接池回收 }
这篇关于【Java爬虫】HttpClient样例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?