HttpClient 3.1 Bypass HTTPS request SSL authentication

Source: Internet
Author: User

One, because the use of HTTPS when sending requests will be involved in the authentication method. But this is not a convenient way to use it. Especially when requesting an external interface, so I wrote a way to skip validation. (for reference)

Second, add the package, here is the package of Commons-httpclient 3.1. General requests with the latest httpclient4.5 can be

<dependency>  <groupId>commons-httpclient</groupId>  <artifactId> Commons-httpclient</artifactid>  <version>3.1</version></dependency>

Three, here we implement 3 classes

1, Myx509trustmanager (this method directly implemented X509trustmanager,x509trustmanager in Javax.net.ssl.X509TrustManager)

There's no need to change anything directly here.

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 (here we need to use the Sslcontext, but also need to rewrite a method to implement 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 {//Add a property here, the main purpose is to get SSL skip authentication private Sslcontext sslcontext = null;     /** * Constructor for Mysecureprotocolsocketfactory. */Public Mysecureprotocolsocketfactory () {}/** * This creates a method to get Sslcontext, import myx509trustmanager for initialization * @re Turn */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 ());  }}/** * Determine get Sslcontext * @return */private Sslcontext Getsslcontext () {if (this.sslcontext        = = null) {This.sslcontext = Createeasysslcontext ();    } return this.sslcontext; }//The following method is basically to bring in the relevant parameters can be */* * (NON-JAVADOC) * * @see Org.apache.commons.httpclient.protocol.ProtocolSock Etfactory#createsocket (java.lang.String, * int, java.net.InetAddress, int) */public Socket Createsocket (S Tring Host, int port, inetaddress clienthost,int clientport) throws IOException, Unknownhostexception {return GetS    Slcontext (). 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 localaddres s, final int localport, final httpconnectionparams params) throws Ioexception,unknownhostexc Eption, Connecttimeoutexception {if (params = = null) {throw new IllegalArgumentException ("Parameters m        Ay not being 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, then is httpclient, here the way to achieve is a single room, as long as the declaration mysecureprotocolsocketfactory join on it can be protocol

Import Org.apache.commons.httpclient.methods.getmethod;import Org.apache.commons.httpclient.protocol.Protocol; Import org.apache.commons.httpclient.protocol.protocolsocketfactory;/* * Tool class with httpclient for post requests */public class Httpclientutil {public static string doget (string url) throws Exception {//Declaration protocolsocketfactory FCT        y = new mysecureprotocolsocketfactory ();        Join the relevant HTTPS request mode Protocol.registerprotocol ("https", New Protocol ("https", Fcty, 443));        The request can be sent 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 (); }   }} 

Four, here is basically completed, in the use of the time as long as the declaration mysecureprotocolsocketfactory join can be protocol, and then you can achieve the validation of the skipped

Transferred from: http://www.cnblogs.com/ll409546297/p/7154542.html

HttpClient 3.1 Bypass HTTPS request SSL authentication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.