HTTPClient官网:http://hc.apache.org/httpcomponents-client-4.5.x/quickstart.html

问题一:明明浏览器请求有数据,可使用HTTPClient输出却为空

  1. import org.apache.http.*;
  2. import org.apache.http.client.*;
  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. import org.junit.Test;
  8.  
  9. @Test
  10. public void httpClientTest1() {
  11. CloseableHttpClient httpclient = HttpClients.createDefault();
  12. try{
  13. String url = "https://www.80s.tw";
  14. HttpGet httpGet = new HttpGet(url);
  15. System.out.println("executing request " + httpGet.getURI());
  16.  
  17. ResponseHandler<String> responseHandler = new ResponseHandler<String>(){
  18. public String handleResponse(final HttpResponse response) throws ClientProtocolException,IOException{
  19. int status = response.getStatusLine().getStatusCode();
  20. if (status >= 200 && status < 300){
  21. HttpEntity entity = response.getEntity();
  22. return entity !=null ? EntityUtils.toString(entity) : null;
  23. }else{
  24. throw new ClientProtocolException("Unexpected response status: " + status);
  25. }
  26. }
  27. };
  28. String responseBody = null;
  29. try {
  30. responseBody = httpclient.execute(httpGet,responseHandler);
  31. } catch (ClientProtocolException e) {
  32. e.printStackTrace();
  33. } catch (IOException e) {
  34. }
  35. System.out.println("-------------------------------------------");
  36. System.out.println(responseBody);
  37. System.out.println("-------------------------------------------");
  38. }finally{
  39. try {
  40. httpclient.close();
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. }

  原因1:访问该网站可能需要证书

  证书解决办法:http://www.cnblogs.com/zhumengke/p/8846912.html

再次请求时导入我们下载的证书

  1. import javax.net.ssl.SSLContext;
  2. import org.apache.http.HttpEntity;
  3. import org.apache.http.client.methods.CloseableHttpResponse;
  4. import org.apache.http.client.methods.HttpGet;
  5. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  6. import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
  7. import org.apache.http.impl.client.CloseableHttpClient;
  8. import org.apache.http.impl.client.HttpClients;
  9. import org.apache.http.ssl.SSLContexts;
  10. import org.apache.http.util.EntityUtils;
  11. import org.junit.Test;
  12. @Test
  13. public void httpTest() {
  14. SSLContext sslcontext = null;
  15. try {
  16. File file = new File("D:/java/jre/lib/security", "jssecacerts");
  17. sslcontext = SSLContexts.custom()
  18. .loadTrustMaterial(file, "changeit".toCharArray(), new TrustSelfSignedStrategy()).build();
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null,
  23. SSLConnectionSocketFactory.getDefaultHostnameVerifier());
  24. CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
  25. try {
  26. HttpGet httpget = new HttpGet("https://www.80s.tw");
  27. System.out.println("Executing request " + httpget.getRequestLine());
  28. CloseableHttpResponse response = httpclient.execute(httpget);
  29. try {
  30. HttpEntity entity = response.getEntity();
  31. System.out.println("----------------------------------------");
  32. System.out.println(response.getStatusLine());
  33. System.out.println(EntityUtils.toString(entity));
  34. EntityUtils.consume(entity);
  35. } finally {
  36. response.close();
  37. }
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. } finally {
  41. try {
  42. httpclient.close();
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. }

  

网络爬虫之HTTPClient的更多相关文章

  1. Java网络爬虫笔记

    Java网络爬虫笔记 HttpClient来代替浏览器发起请求. select找到的是元素,也就是elements,你想要获取具体某一个属性的值,还是要用attr("")方法.标签 ...

  2. 基于HttpClient实现网络爬虫~以百度新闻为例

    转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/40891791 基于HttpClient4.5实现网络爬虫请訪问这里:http:/ ...

  3. Httpclient: 多层翻页网络爬虫实战(以搜房网为例)

    参考:http://blog.csdn.net/qy20115549/article/details/52912532 一.创建数据表 #创建表:用来存储url地址信息 create table so ...

  4. 【网络爬虫】【java】微博爬虫(二):如何抓取HTML页面及HttpClient使用

    一.写在前面 上篇文章以网易微博爬虫为例,给出了一个很简单的微博爬虫的爬取过程,大概说明了网络爬虫其实也就这么回事,或许初次看到这个例子觉得有些复杂,不过没有关系,上篇文章给的例子只是让大家对爬虫过程 ...

  5. Java网络爬虫 HttpClient

    简介 : HttpClient是Apache Jakarta Common下的子项目,用于提供高效的,功能丰富的支持HTTP协议的客户编程工具包,其主要功能如下: 实现了所有HTTP的方法 : GET ...

  6. Atitit 网络爬虫与数据采集器的原理与实践attilax著 v2

    Atitit 网络爬虫与数据采集器的原理与实践attilax著 v2 1. 数据采集1 1.1. http lib1 1.2. HTML Parsers,1 1.3. 第8章 web爬取199 1 2 ...

  7. Java开发、网络爬虫、自然语言处理、数据挖掘简介

    一.java开发 (1) 应用开发,即Java SE开发,不属于java的优势所在,所以市场占有率很低,前途也不被看好. (2) web开发,即Java Web开发,主要是基于自有或第三方成熟框架的系 ...

  8. 开源的49款Java 网络爬虫软件

    参考地址 搜索引擎 Nutch Nutch 是一个开源Java 实现的搜索引擎.它提供了我们运行自己的搜索引擎所需的全部工具.包括全文搜索和Web爬虫. Nutch的创始人是Doug Cutting, ...

  9. java假设模拟请求重新启动路由器(网络爬虫经常使用),还有java怎样下载图片

    我们假设在公司或家里使用网络爬虫去抓取自己索要的一些数据的时候,经常对方的站点有defence机制,会给你的http请求返回500错误,仅仅要是同样IP就请求不到数据,这时候我们仅仅能去重新启动路由器 ...

随机推荐

  1. VSCode支持jsx自动补全

    点击settings.json中编辑, 把这段话加上去就可以了 "emmet.includeLanguages": { "javascript": " ...

  2. navicat安装与激活

    原文网址:https://www.jianshu.com/p/5f693b4c9468?mType=Group 一.Navicat Premium 12下载 Navicat Premium 12是一套 ...

  3. [LeetCode]-011-Roman_to_Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  4. @清晰掉 qsort()

    qsort函数描述: http://www.cnblogs.com/sooner/archive/2012/04/18/2455011.html qsort()函数实现: /*** *qsort.c ...

  5. 点云网格化算法---MPA

    MPA网格化算法思路 第一步:初始化一个种子三角面.(随机选点,基于该点进行临近搜索到第二点:在基于该线段中点临近搜索到第三点) 图1 第二步:在种子三角面的基础上,进行面片的扩充,利用边的中点进行临 ...

  6. nginx调优buffer参数设置

    内容来自 https://blog.tanteng.me/2016/03/nginx-buffer-params/.有空再详细了解 Nginx性能调优之buffer参数设置 打开Nginx的error ...

  7. 基本vim快捷键

    w,e等单词间跳转,英文字符(除了_),数字,符号,空格 都被vim独立看成独立的词

  8. 【c++进阶:c++ 顺序容器vector,string,deque,list,forward_list,array常用性质】

    常用5种顺序容器性质: https://blog.csdn.net/oil_you/article/details/82821833 关于deque https://www.cnblogs.com/L ...

  9. python读取文件乱码

    方法一:使用codecs import codecs f = codecs.open('nlpir/Readme.txt','r','GBK') line = f.readline() while l ...

  10. 【SSH】---【Struts2、Hibernate5、Spring4】【散点知识】

    一.Struts21.1.Struts2的概念Struts2是一个用来开发MVC应用程序的框架,它提供了Web应用程序开发过程中的一些常见问题的解决方案:    ->对来自用户的输入数据进行合法 ...