网络爬虫之HTTPClient
HTTPClient官网:http://hc.apache.org/httpcomponents-client-4.5.x/quickstart.html
问题一:明明浏览器请求有数据,可使用HTTPClient输出却为空
- import org.apache.http.*;
- import org.apache.http.client.*;
- 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 org.junit.Test;
- @Test
- public void httpClientTest1() {
- CloseableHttpClient httpclient = HttpClients.createDefault();
- try{
- String url = "https://www.80s.tw";
- HttpGet httpGet = new HttpGet(url);
- System.out.println("executing request " + httpGet.getURI());
- ResponseHandler<String> responseHandler = new ResponseHandler<String>(){
- public String handleResponse(final HttpResponse response) throws ClientProtocolException,IOException{
- int status = response.getStatusLine().getStatusCode();
- if (status >= 200 && status < 300){
- HttpEntity entity = response.getEntity();
- return entity !=null ? EntityUtils.toString(entity) : null;
- }else{
- throw new ClientProtocolException("Unexpected response status: " + status);
- }
- }
- };
- String responseBody = null;
- try {
- responseBody = httpclient.execute(httpGet,responseHandler);
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- }
- System.out.println("-------------------------------------------");
- System.out.println(responseBody);
- System.out.println("-------------------------------------------");
- }finally{
- try {
- httpclient.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
原因1:访问该网站可能需要证书
证书解决办法:http://www.cnblogs.com/zhumengke/p/8846912.html
再次请求时导入我们下载的证书
- import javax.net.ssl.SSLContext;
- import org.apache.http.HttpEntity;
- import org.apache.http.client.methods.CloseableHttpResponse;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
- import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
- import org.apache.http.impl.client.CloseableHttpClient;
- import org.apache.http.impl.client.HttpClients;
- import org.apache.http.ssl.SSLContexts;
- import org.apache.http.util.EntityUtils;
- import org.junit.Test;
- @Test
- public void httpTest() {
- SSLContext sslcontext = null;
- try {
- File file = new File("D:/java/jre/lib/security", "jssecacerts");
- sslcontext = SSLContexts.custom()
- .loadTrustMaterial(file, "changeit".toCharArray(), new TrustSelfSignedStrategy()).build();
- } catch (Exception e) {
- e.printStackTrace();
- }
- SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null,
- SSLConnectionSocketFactory.getDefaultHostnameVerifier());
- CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
- try {
- HttpGet httpget = new HttpGet("https://www.80s.tw");
- System.out.println("Executing request " + httpget.getRequestLine());
- CloseableHttpResponse response = httpclient.execute(httpget);
- try {
- HttpEntity entity = response.getEntity();
- System.out.println("----------------------------------------");
- System.out.println(response.getStatusLine());
- System.out.println(EntityUtils.toString(entity));
- EntityUtils.consume(entity);
- } finally {
- response.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- httpclient.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
网络爬虫之HTTPClient的更多相关文章
- Java网络爬虫笔记
Java网络爬虫笔记 HttpClient来代替浏览器发起请求. select找到的是元素,也就是elements,你想要获取具体某一个属性的值,还是要用attr("")方法.标签 ...
- 基于HttpClient实现网络爬虫~以百度新闻为例
转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/40891791 基于HttpClient4.5实现网络爬虫请訪问这里:http:/ ...
- Httpclient: 多层翻页网络爬虫实战(以搜房网为例)
参考:http://blog.csdn.net/qy20115549/article/details/52912532 一.创建数据表 #创建表:用来存储url地址信息 create table so ...
- 【网络爬虫】【java】微博爬虫(二):如何抓取HTML页面及HttpClient使用
一.写在前面 上篇文章以网易微博爬虫为例,给出了一个很简单的微博爬虫的爬取过程,大概说明了网络爬虫其实也就这么回事,或许初次看到这个例子觉得有些复杂,不过没有关系,上篇文章给的例子只是让大家对爬虫过程 ...
- Java网络爬虫 HttpClient
简介 : HttpClient是Apache Jakarta Common下的子项目,用于提供高效的,功能丰富的支持HTTP协议的客户编程工具包,其主要功能如下: 实现了所有HTTP的方法 : GET ...
- Atitit 网络爬虫与数据采集器的原理与实践attilax著 v2
Atitit 网络爬虫与数据采集器的原理与实践attilax著 v2 1. 数据采集1 1.1. http lib1 1.2. HTML Parsers,1 1.3. 第8章 web爬取199 1 2 ...
- Java开发、网络爬虫、自然语言处理、数据挖掘简介
一.java开发 (1) 应用开发,即Java SE开发,不属于java的优势所在,所以市场占有率很低,前途也不被看好. (2) web开发,即Java Web开发,主要是基于自有或第三方成熟框架的系 ...
- 开源的49款Java 网络爬虫软件
参考地址 搜索引擎 Nutch Nutch 是一个开源Java 实现的搜索引擎.它提供了我们运行自己的搜索引擎所需的全部工具.包括全文搜索和Web爬虫. Nutch的创始人是Doug Cutting, ...
- java假设模拟请求重新启动路由器(网络爬虫经常使用),还有java怎样下载图片
我们假设在公司或家里使用网络爬虫去抓取自己索要的一些数据的时候,经常对方的站点有defence机制,会给你的http请求返回500错误,仅仅要是同样IP就请求不到数据,这时候我们仅仅能去重新启动路由器 ...
随机推荐
- VSCode支持jsx自动补全
点击settings.json中编辑, 把这段话加上去就可以了 "emmet.includeLanguages": { "javascript": " ...
- navicat安装与激活
原文网址:https://www.jianshu.com/p/5f693b4c9468?mType=Group 一.Navicat Premium 12下载 Navicat Premium 12是一套 ...
- [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 ...
- @清晰掉 qsort()
qsort函数描述: http://www.cnblogs.com/sooner/archive/2012/04/18/2455011.html qsort()函数实现: /*** *qsort.c ...
- 点云网格化算法---MPA
MPA网格化算法思路 第一步:初始化一个种子三角面.(随机选点,基于该点进行临近搜索到第二点:在基于该线段中点临近搜索到第三点) 图1 第二步:在种子三角面的基础上,进行面片的扩充,利用边的中点进行临 ...
- nginx调优buffer参数设置
内容来自 https://blog.tanteng.me/2016/03/nginx-buffer-params/.有空再详细了解 Nginx性能调优之buffer参数设置 打开Nginx的error ...
- 基本vim快捷键
w,e等单词间跳转,英文字符(除了_),数字,符号,空格 都被vim独立看成独立的词
- 【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 ...
- python读取文件乱码
方法一:使用codecs import codecs f = codecs.open('nlpir/Readme.txt','r','GBK') line = f.readline() while l ...
- 【SSH】---【Struts2、Hibernate5、Spring4】【散点知识】
一.Struts21.1.Struts2的概念Struts2是一个用来开发MVC应用程序的框架,它提供了Web应用程序开发过程中的一些常见问题的解决方案: ->对来自用户的输入数据进行合法 ...