HttpClient

网络爬虫就是用程序帮助我们访问网络上的资源,我们一直以来都是使用HTTP协议访问互联网的网页,网络爬虫需要编写程序,在这里使用同样的HTTP协议访问网页。

这里使用Java的HTTP协议客户端 HttpClient这个技术,来实现抓取网页数据。


GET请求

package cn.itcast.crawler.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访问地址HttpGet httpGet = new HttpGet("http://www.itcast.cn");CloseableHttpResponse response = null;try {//使用HttpClient发起请求,获取responseresponse = 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 {//关闭responsetry {response.close();} catch (IOException e) {e.printStackTrace();}try {httpClient.close();} catch (IOException e) {e.printStackTrace();}}}
}



GET请求

带参数的GET请求


package cn.itcast.crawler.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;
import java.net.URISyntaxException;public class HttpGetParamTest {public static void main(String[] args) throws Exception {//创建HttpClient对象CloseableHttpClient httpClient = HttpClients.createDefault();//设置请求地址是:http://yun.itheima.com/search?keys=Java//创建URIBuilderURIBuilder 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发起请求,获取responseresponse = 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 {//关闭responsetry {response.close();} catch (IOException e) {e.printStackTrace();}try {httpClient.close();} catch (IOException e) {e.printStackTrace();}}}
}

HttpClient ||GET请求||带参数的GET请求相关推荐

  1. POST请求||带参数的POST请求

    POST请求 HttpPostTest.java package cn.itcast.crawler.test;import org.apache.http.client.methods.Closea ...

  2. 使用httpclient进行 Http get请求带参数cookie

    使用httpclient进行 Http get请求带参数cookie 常用的工具类,所有的包配置如下 <!--解析json使用--><dependency><groupI ...

  3. HttpClient Get请求带参数

    /** HttpClient Get请求带参数*/@Testpublic void fun1() throws ClientProtocolException, IOException {// 1.创 ...

  4. java http post 传参数_HttpClient之带参数的post请求

    具体代码: package cn.itcast.crawler.test; import org.apache.http.HttpEntity; import org.apache.http.Name ...

  5. python get请求带参数_python_request的安装及模拟json的post请求及带参数的get请求

    一.Requests模块安装 安装方式一: 执行 pip install -U requests 联网安装requests 安装方式二: 进入https://pypi.org/project/requ ...

  6. requests之get请求带参数示例详解

    具体见以下详细步骤 获取响应数据 获取请求头 获取响应头.状态码 有两点需要引起注意: a.响应结果一般有三种格式:HTML.json.text b.text方法返回的是字符串格式的内容 获取cook ...

  7. 带参数的Get请求与不带参数的Get请求

    不带参数的Get请求 [{"description": "这是一个不带参数的get请求","request": {"uri&quo ...

  8. 【安卓开发】Android初级开发(okhttp3发送带header与带参数的GET请求)

    1.首先需要先加入相应依赖 dependencies{implementation 'com.squareup.okhttp3:okhttp:3.13.1' implementation 'com.s ...

  9. React Native ---fetch 之GET请求带参数

    网上关于其GET请求的用例全是没有带参数的,带参数的又全部是POST请求下面给出带参数的做法. export function get(url,params){if (params) {let par ...

最新文章

  1. devc++鼠标变成了光标_Excel填充别再用鼠标拖拉了!用这4个方法,效率至少高10倍!...
  2. php导出服务器表格乱码,phpExcel导出, 在本地正常,在服务器乱码解决办法
  3. 结合Pandas中的多个数据集
  4. 清华大学自主研究的计算机,清华大学人工智能研究院成立基础理论研究中心,朱军教授任中心主任...
  5. Joomla css类后缀
  6. Laravel 速记表
  7. ARP欺骗+DNS欺骗
  8. vue-echarts教程
  9. python 等频分箱_数据分箱:等频分箱,等距分箱,卡方分箱,计算WOE、IV
  10. 2.5D 组态案例合集 | 智慧园区、数据中心、SMT 生产线、汽车制造
  11. SQL注入大闯关笔记
  12. 移植st官方usb-hid程序出现babble detected错误
  13. 开源WebGIS开发——如何创建一张地图
  14. 大平台压榨亏损2000万怎么办?换流量变现策略才是王道!
  15. DuerOS智能设备激活数突破1亿 小度购物节再掀热潮
  16. APPSO 视频号-空白昵称(全系统通用)
  17. RGB转8色16色256色
  18. HTML光速了解!!!
  19. 哔哩下载姬 - DownKyi
  20. 计算机族的健康饮品,健康生活习惯,护眼诀窍---多 活 十 年

热门文章

  1. 使用录制宏制作工资条
  2. 【工具篇】接口测试神器 -- Postman 入门教程
  3. Pots POJ - 3414
  4. (二)Maven的安装与环境配置
  5. 【P1326】超级教主
  6. POJ 2230 DFS
  7. Device Tree(二):基本概念
  8. 打断点是什么意思_黄金走势分析?股票所说的大盘几千点,是指的什么意思
  9. Javascript数组常见的方法
  10. jquery 事件:2