【网络爬虫】Httpclient4.X中使用HTTPS的方法采集12306网站
HttpClient请求https的实例:
package train; import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager; import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams; public class train { public static void main(String args[]) { try { HttpClient httpclient = new DefaultHttpClient();
// Secure Protocol implementation.
SSLContext ctx = SSLContext.getInstance("SSL");
// Implementation of a trust manager for X509 certificates
X509TrustManager tm = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
} public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx); ClientConnectionManager ccm = httpclient.getConnectionManager();
// register https protocol in httpclient's scheme registry
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf)); HttpGet httpget = new HttpGet(
"https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=ADULT&queryDate=2016-08-23&from_station=BJP&to_station=TJP");
HttpParams params = httpclient.getParams(); System.out.println("Request URL:" + httpget.getURI());
ResponseHandler responseHandler = new BasicResponseHandler(); String responseBody = (String) httpclient.execute(httpget, responseHandler); System.out.println(responseBody); // Create a response handler } catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace(); }
}
}
【网络爬虫】Httpclient4.X中使用HTTPS的方法采集12306网站的更多相关文章
- httpclient4.X中使用HTTPS的方法采集12306网站
HttpClient请求https的实例: package train; import java.io.IOException; import java.security.NoSuchAlgorith ...
- 网络爬虫深究-初识HTTP和https常识
网络爬虫专题--HTTP基本原理 URI和URL.URN的关系 超文本 HTTP和HTTPS URI和URL 首先,我们来了解一下URI和URL,URI,即统一资源标志符,URL,即统一资源定位符. ...
- Python网络爬虫 - 爬取中证网银行相关信息
最终版:07_中证网(Plus -Pro).py # coding=utf-8 import requests from bs4 import BeautifulSoup import io impo ...
- 网络爬虫框架Heritrix中Modules的各项说明
1)Select Crawl Scope:Crawl Scope 用于配置当前应该在什么范围内抓取网页链接.例如选择 BroadScope 则表示当前的抓取范围不受限制,选择 HostScope 则表 ...
- 元类理解与元类编程 《Python3网络爬虫开发》中第九章代理的使用代码Crawler中代码的理解
__new__与__init__的理解 __new__()方法是在创建实例之前被调用的,它的作用是创建一个实例,然后返回该实例对象,它是一个静态方法. __init__() 当实例被创建完成之后被调用 ...
- 02.python网络爬虫第二弹(http和https协议)
一.HTTP协议 1.官方概念: HTTP协议是 Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(www.world wide web) 服务器传输超 ...
- 网络爬虫--requests库中两个重要的对象
当我们使用resquests.get()时,返回的时response的对象,他包含服务器返回的所有信息,也包含请求的request的信息. 首先: response对象的属性有以下几个, r.stat ...
- Pyhton网络爬虫实例_豆瓣电影排行榜_BeautifulSoup4方法爬取
-----------------------------------------------------------学无止境------------------------------------- ...
- Pyhton网络爬虫实例_豆瓣电影排行榜_Xpath方法爬取
-----------------------------------------------------------学无止境------------------------------------- ...
随机推荐
- [Java]在窗口界面上画出硬盘中图片文件
利用类javax.swing.JPanel来在窗口界面上画图.图片文件通过javax.imageio.ImageIO类来获取. import java.awt.Graphics; import jav ...
- javascript正则中ASCII与unicode
正则表达式中允许直接利用ASCII和Unicode编码来查找我们相应的字符串. ASCII: 下面是检索ASCII编码在x41-x7a范围内的所有匹配的字符串.(x41,x7a为十六进制) var s ...
- 流畅的python第十八章使用asyncio包处理并发
对比一个简单的多线程程序和对应的 asyncio 版,说明多线程和异步任务之间的关系asyncio.Future 类与 concurrent.futures.Future 类之间的区别摒弃线程或进程, ...
- 【云计算】OpenStack Horizon DashBoard定制化,完整实现前后台交互
项目代码见GitHub:https://github.com/junneyang/openstack-customization-example 参考资料: Install and configure ...
- 转: git的图文使用教程(巨详细)
转自: http://blog.jobbole.com/78960/ Git使用教程 一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集 ...
- java 过滤器补充
多个Filter按照在配置文件中配置的filter顺序执行. 在web.xml文件中配置该Filter,使用init-param元素为该Filter配置参数,init-param可接受如下两个子元素: ...
- TestNG系列之二:TestNG套件测试
测试套件的测试是为了测试软件程序的行为或一系列行为的情况下,是一个集合.在TestNG,我们不能定义一套测试源代码,但它代表的套件是一个XML文件执行特征.这也允许灵活的配置要运行的测试.套件可以包含 ...
- WebService 之 WSDL详解
WSDL (Web Services Description Language,Web服务描述语言)是一种XML Application,他将Web服务描述定义为一组服务访问点,客户端可以通过这些服务 ...
- windows下流媒体nginx-rmtp-module服务器搭建及java程序调用fmpeg将rtsp转rtmp直播流【转】
https://github.com/illuspas/nginx-rtmp-win32 http://bashell.sinaapp.com/archives/build-nginx-rtmp-mo ...
- Unity AssetServer小笔记
普及一下Asset Server知识: Unity的Asset Server,其实就是一个纯PostgreSQL数据库.Unity操作Asset Server,就是一个数据库客户端操作,但是Unity ...