博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
httpclient跳过https请求的验证
阅读量:6150 次
发布时间:2019-06-21

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

一、因为在使用https发送请求的时候会涉及,验证方式。但是这种方式在使用的时候很不方便。特别是在请求外部接口的时候,所以这我写了一个跳过验证的方式。(供参考)

二、加入包,这里用的是commons-httpclient 3.1 的包。一般请求采用最新的httpclient4.5就可以了

commons-httpclient
commons-httpclient
3.1

三、这里我们实现3个类

1、MyX509TrustManager(这个方法直接实现X509TrustManager,X509TrustManager在javax.net.ssl.X509TrustManager里面)

这里直接实现不用改任何东西

import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import javax.net.ssl.X509TrustManager;public class MyX509TrustManager implements X509TrustManager {    /* (non-Javadoc)    * @see javax.net.ssl.X509TrustManager#checkClientTrusted(java.security.cert.X509Certificate[], java.lang.String)    */    public void checkClientTrusted(X509Certificate[] arg0, String arg1)            throws CertificateException {    }    /* (non-Javadoc)     * @see javax.net.ssl.X509TrustManager#checkServerTrusted(java.security.cert.X509Certificate[], java.lang.String)     */    public void checkServerTrusted(X509Certificate[] arg0, String arg1)            throws CertificateException {    }    /* (non-Javadoc)     * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()     */    public X509Certificate[] getAcceptedIssuers() {        return null;    }}

2、MySecureProtocolSocketFactory(这里我们需要用到SSLContext,还需要改写一个实现SecureProtocolSocketFactory的方法)

import java.io.IOException;import java.net.InetAddress;import java.net.Socket;import java.net.UnknownHostException;import javax.net.ssl.SSLContext;import javax.net.ssl.TrustManager;import org.apache.commons.httpclient.ConnectTimeoutException;import org.apache.commons.httpclient.HttpClientError;import org.apache.commons.httpclient.params.HttpConnectionParams;import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;public class MySecureProtocolSocketFactory implements SecureProtocolSocketFactory {    //这里添加一个属性,主要目的就是来获取ssl跳过验证    private SSLContext sslContext = null;    /**     * Constructor for MySecureProtocolSocketFactory.     */    public MySecureProtocolSocketFactory() {    }    /**     * 这个创建一个获取SSLContext的方法,导入MyX509TrustManager进行初始化     * @return     */    private static SSLContext createEasySSLContext() {        try {            SSLContext context = SSLContext.getInstance("SSL");            context.init(null, new TrustManager[] { new MyX509TrustManager() },                    null);            return context;        } catch (Exception e) {            throw new HttpClientError(e.toString());        }    }    /**     * 判断获取SSLContext     * @return     */    private SSLContext getSSLContext() {        if (this.sslContext == null) {            this.sslContext = createEasySSLContext();        }        return this.sslContext;    }    //后面的方法基本上就是带入相关参数就可以了    /*     * (non-Javadoc)     *     * @see org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String,     *      int, java.net.InetAddress, int)     */    public Socket createSocket(String host, int port, InetAddress clientHost,int clientPort) throws IOException, UnknownHostException {        return getSSLContext().getSocketFactory().createSocket(host, port,clientHost, clientPort);    }    /*     * (non-Javadoc)     *     * @see org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String,     *      int, java.net.InetAddress, int,     *      org.apache.commons.httpclient.params.HttpConnectionParams)     */    public Socket createSocket(final String host, final int port,final InetAddress localAddress, final int localPort,                               final HttpConnectionParams params) throws IOException,UnknownHostException, ConnectTimeoutException {        if (params == null) {            throw new IllegalArgumentException("Parameters may not be null");        }        int timeout = params.getConnectionTimeout();        if (timeout == 0) {            return createSocket(host, port, localAddress, localPort);        } else {            return ControllerThreadSocketFactory.createSocket(this, host, port,localAddress, localPort, timeout);        }    }    /*     * (non-Javadoc)     *     * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int)     */    public Socket createSocket(String host, int port) throws IOException,UnknownHostException {        return getSSLContext().getSocketFactory().createSocket(host, port);    }    /*     * (non-Javadoc)     *     * @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean)     */    public Socket createSocket(Socket socket, String host, int port,boolean autoClose) throws IOException, UnknownHostException {        return getSSLContext().getSocketFactory().createSocket(socket, host,port, autoClose);    }}

3、然后就是httpclient了,这里实现的方式很单间了,只要声明MySecureProtocolSocketFactory加入就可以了Protocol

import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.protocol.Protocol;import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;/* * 利用HttpClient进行post请求的工具类 */public class HttpClientUtil {    public static String doGet(String url) throws Exception {        //声明        ProtocolSocketFactory fcty = new MySecureProtocolSocketFactory();        //加入相关的https请求方式        Protocol.registerProtocol("https", new Protocol("https", fcty, 443));        //发送请求即可        org.apache.commons.httpclient.HttpClient httpclient = new org.apache.commons.httpclient.HttpClient();        GetMethod httpget = new GetMethod(url);        System.out.println("======url:" + url);        try {            httpclient.executeMethod(httpget);            return httpget.getResponseBodyAsString();        } catch (Exception ex) {            ex.printStackTrace();            throw new Exception(ex.getMessage());        } finally {            httpget.releaseConnection();        }    }}

四、这里基本上就完成了,在会用的时候只要声明MySecureProtocolSocketFactory加入就可以了Protocol,然后就可以实现验证的跳过过了

 

转载于:https://www.cnblogs.com/ll409546297/p/7154542.html

你可能感兴趣的文章
Windows安装TensorFlow遇到错误
查看>>
OC调用Swift 整理步骤!总结别人的!方便自己查找!
查看>>
Array之foreach
查看>>
java.lang.NumberFormatException: For input string:""
查看>>
日期和时间
查看>>
LeetCode: Scramble String
查看>>
vue使用marked.js实现markdown转html并提取标题生成目录
查看>>
【noi 2.2_8758】2的幂次方表示(递归)
查看>>
记录一下最近的感受
查看>>
2153: 2018湖南多校第二场-20180407(网络同步赛)
查看>>
最大子段和模板
查看>>
POJ 1258 + POJ 1287 【最小生成树裸题/矩阵建图】
查看>>
ExtJs ajax.request 的变化
查看>>
设置checkbox选中,设置radio选中,根据值设置checkbox选中,checkbox勾选
查看>>
使用urllib2的HttpResponse导致内存不回收(内存泄漏)
查看>>
GM、VP、FVP、CIO都是什么职位?
查看>>
我最常用的几个Xcode快键键
查看>>
rsync远程同步
查看>>
页面定制CSS代码初探(五):给每篇文章最后加上'<完>'
查看>>
实验1 OpenGL初识
查看>>