博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【网络爬虫】Httpclient4.X中使用HTTPS的方法采集12306网站
阅读量:5912 次
发布时间:2019-06-19

本文共 2965 字,大约阅读时间需要 9 分钟。

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();        }    }}

  

 

转载地址:http://kdmpx.baihongyu.com/

你可能感兴趣的文章
我的友情链接
查看>>
PowerShell Switch判断语句示例
查看>>
《Spring实战》第四版读书笔记 第一章 Spring之旅
查看>>
那些年,一起学的Java 3-3
查看>>
那些年,一起学的Java 2-4
查看>>
Java中的多态和C#中的多态的区别
查看>>
UIView之【UIViewContentMode】
查看>>
yum 及手动编译rpm包
查看>>
使用Maven运行 MyBatis Generator
查看>>
7-设计模式-代理模式
查看>>
RedHat已更改其开源许可规则
查看>>
Android零基础入门第29节:善用TableLayout表格布局,事半功倍
查看>>
element-ui 的 table后端排序
查看>>
redis集群搭建
查看>>
linux重定向
查看>>
红包生成的模拟器2018今日头条秋招
查看>>
管道符和作业控制,shell变量和环境变量配置文件
查看>>
DirectX3D设备丢失(lost device)的处理(一)
查看>>
来自田野的回音——《背过身去的大娘娘》的读后感范文2600字
查看>>
LNMP架构 (Ⅱ)——nginx相关配置、nginx代理
查看>>