1 package com.lw.httpclient.test;
2 import org.apache.http.client.methods.CloseableHttpResponse;
3 import org.apache.http.client.methods.HttpGet;
4 import org.apache.http.impl.client.CloseableHttpClient;
5 import org.apache.http.impl.client.HttpClients;
6 import org.apache.http.util.EntityUtils;
7
8 public class HttpClientTest {
9 public static void main(String[] args) throws Exception {
10 // TODO Auto-generated method stub
11 get1();
12 get2();
13 }
14 /**
15 * 获取指定链接的网页的内容【初级版】
16 * @throws Exception
17 */
18 public static void get1()throws Exception{
19 //HttpClient hc=new DefaultHttpClient();
20 String url="http://www.budejie.com";
21 url="http://www.btba.com.cn";//网站限制爬,这种方式不再实用。
22 CloseableHttpClient chc=HttpClients.createDefault();
23 HttpGet hg=new HttpGet(url);
24 CloseableHttpResponse chp=chc.execute(hg);
25 System.out.println(EntityUtils.toString(chp.getEntity(),"UTF-8"));
26 }
27 /**
28 * 通过模拟浏览器获取指定链接的页面
29 * @throws Exception
30 */
31 public static void get2()throws Exception{
32 CloseableHttpClient closeableHttpClient=HttpClients.createDefault();
33 String url="http://www.btba.com.cn";
34 HttpGet httpGet=new HttpGet(url);
35 //设置请求头,模拟浏览器访问
36 httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0");
37 CloseableHttpResponse chr=closeableHttpClient.execute(httpGet);
38 System.out.println(EntityUtils.toString(chr.getEntity(),"UTF-8"));
39 }
40 }

未完待续

将会添加如何解析获取到的内容,得到自己想要的部分。。

最新文章

  1. JS详细入门教程(上)
  2. [Tool] SourceTree操作中遇到错误(Filename too long)的解决方案
  3. DOM_04之常用对象及BOM
  4. poj 1276 多重背包
  5. JS常用的设计模式(12)—— 迭代器模式
  6. jquery获取div距离顶部的距离
  7. C++遍历目录,并把目录里超过7天的文件删除(跨平台windows&linux)
  8. vbox下安装 linux 64 bit出现“kernel requires an x86_64 cpu
  9. hql中不能写count(1)能够写count(a.id)
  10. ssh 实体关系分析确立(ER图-实体关系图)
  11. vuejs(2.0)基础笔记
  12. redis 系列11 列表对象
  13. oracle利用redo恢复
  14. Linux中Hadoop的环境搭建
  15. Python - 去除list中的空字符
  16. java date HHmmss hhmmss
  17. Vue计算属性的用法
  18. gj12-1 协程和异步io
  19. ubuntu 添加CDROM安装源
  20. CC3200-LAUNCHXL驱动不能正常识别的问题

热门文章

  1. Hi3559AV100 NNIE开发(5)mobilefacenet.wk仿真成功量化及与CNN_convert_bin_and_print_featuremap.py输出中间层数据对比过程
  2. Android Studio 安装并使用genymotion
  3. Java 8的这些新特性,不一样的全新版本(万字长文详细说明)
  4. P1423_小玉在游泳(JAVA语言)
  5. Apache JMeter 5.4.1 Build Development
  6. WPF 反射加载Geometry几何图形数据图标
  7. 一次 outline 去除经验(非继承属性,看着像继承)
  8. 推荐一份Web 工程师的前端书单
  9. C# net Emgu.CV.World 人脸识别 根据照片将人脸抠图出来。
  10. 给我一个shell我能干翻你内网