网络爬虫之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就请求不到数据,这时候我们仅仅能去重新启动路由器 ...
随机推荐
- jQery Datatables回调函数中文
Datatables——回调函数 ------------------------------------------------- fnCookieCallback:还没有使用过 $(documen ...
- 常见的NullPointerException总结
NullPointerException在这里简称为NPE 通过一些实例总结下常见的NPE问题: 1. 自动拆箱抛NPE 实体类: public class User { private String ...
- CSS 手动画热销小图标
效果图 HTML 标签 <div class="main"> <div class="small"> <div class=&qu ...
- 【Django】搭建Django administration并登录
Python自带一个后台管理系统,这个后台管理系统搭建与登陆呢? 新建项目Django_Admin 文件结构目录如下: 创建APP 在pycharm下方的terminal终端中输入命令: python ...
- Java 使用反射给属性赋值
package com.nf147.manage.spring; import java.lang.reflect.Field; public class Cat { private String n ...
- docker 容器不能联网
Unknown Endpoint: Inaccessible host: `dynamodb.us-east-1.amazonaws.com' 别慌!Docker容器内不能联网的6种解决方案 注:下面 ...
- nginx利用try_files实现多个源
比如一个视频网站,视频资源分散在几台机器上,但是给用要提供统一的IP,路径,端口.就需要nginx,先检查本机是否有改文件,如果没有就代理其他地方 location / { root /data/ww ...
- Android NDK下载
http://dl.google.com/android/ndk/android-ndk-r10d-linux-x86_64.bin https://dl.google.com/android/rep ...
- 红帽虚拟化RHEV-安装RHEV-M
目录 目录 前言 软件环境 时间同步 更新系统 安装并配置RHEV-M 添加域并为用户授权远程登陆 安装rhevm报告 安装Spice协议 最后 前言 在红帽虚拟化RHEV-架构简介篇中介绍了RHEV ...
- Unity3D-Canvas-UIScaleMode
UnityCanvasUI自适应部分 用Canvas下的自适应UI Scale Mode: Constant Pixel Size:固定像素大小,无论如何UI怎么样就怎么样,只能通过 Scale Fa ...