Httpsclient automatically obtain certificates without certificate access

Source: Internet
Author: User

The first implementation is to write a class that implements the interface secureprotocolsocketfactory.

/***classname:bcde *date:2015 February 26 pm 4:51:01 * *@authorLimh *@sinceJDK 1.7*/Importjava.io.IOException;Importjava.net.InetAddress;Importjava.net.InetSocketAddress;ImportJava.net.Socket;Importjava.net.SocketAddress;Importjava.net.UnknownHostException;Importjava.security.KeyManagementException;Importjava.security.NoSuchAlgorithmException;Importjava.security.cert.CertificateException;Importjava.security.cert.X509Certificate;Importjavax.net.SocketFactory;ImportJavax.net.ssl.SSLContext;ImportJavax.net.ssl.TrustManager;ImportJavax.net.ssl.X509TrustManager;Importorg.apache.commons.httpclient.ConnectTimeoutException;ImportOrg.apache.commons.httpclient.params.HttpConnectionParams;Importorg.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;/*** MySecureProtocolSocketFactory.java.java Create on 2012-9-26 PM 1:15:03 * * * Copyright (c) by MTA. * *@authorLmeteor * @Email [Email protected] * @description Custom socket Factory implementation automatically accepts certificates *@version1.0*/ Public classMysecureprotocolsocketfactoryImplementsSecureprotocolsocketfactory {PrivateSslcontext Sslcontext =NULL; PrivateSslcontext Createsslcontext () {Sslcontext Sslcontext=NULL; Try{Sslcontext= Sslcontext.getinstance ("SSL"); Sslcontext.init (NULL,                    NewTrustmanager[] {NewTrustanytrustmanager ()},Newjava.security.SecureRandom ()); } Catch(nosuchalgorithmexception e) {e.printstacktrace (); } Catch(keymanagementexception e) {e.printstacktrace (); }        returnSslcontext; }    PrivateSslcontext Getsslcontext () {if( This. Sslcontext = =NULL) {             This. Sslcontext =Createsslcontext (); }        return  This. Sslcontext; }     PublicSocket Createsocket (socket socket, String host,intPort,BooleanAutoClose)throwsIOException, unknownhostexception {returnGetsslcontext (). Getsocketfactory (). Createsocket (socket, host, port, AutoClose); }     PublicSocket Createsocket (String host,intPortthrowsIOException, unknownhostexception {returnGetsslcontext (). Getsocketfactory (). Createsocket (host, Port); }     PublicSocket Createsocket (String host,intPort, inetaddress clienthost,intClientPort)throwsIOException, unknownhostexception {returnGetsslcontext (). Getsocketfactory (). Createsocket (host, Port, ClientHost, ClientPort); }     PublicSocket Createsocket (String host,intPort, inetaddress localaddress,intLocalPort, Httpconnectionparams params)throwsIOException, Unknownhostexception, connecttimeoutexception {if(Params = =NULL) {            Throw NewIllegalArgumentException ("Parameters May is not NULL"); }        intTimeout =params.getconnectiontimeout (); Socketfactory socketfactory=Getsslcontext (). Getsocketfactory (); if(Timeout = 0) {            returnSocketfactory.createsocket (host, Port, localaddress, LocalPort); } Else{Socket Socket=Socketfactory.createsocket (); SocketAddress localaddr=Newinetsocketaddress (localaddress, LocalPort); SocketAddress remoteaddr=Newinetsocketaddress (host, Port);            Socket.bind (LOCALADDR);            Socket.connect (remoteaddr, timeout); returnsocket; }    }    //Customizing Private Classes    Private Static classTrustanytrustmanagerImplementsX509trustmanager { Public voidcheckclienttrusted (x509certificate[] chain, String authtype)throwscertificateexception {} Public voidcheckservertrusted (x509certificate[] chain, String authtype)throwscertificateexception {} Publicx509certificate[] Getacceptedissuers () {return Newx509certificate[] {}; }    }}

When calling, you only need to register the HTTPS protocol object that you just created before generating httpclient, and then httpsclient usage is normal.

New mysecureprotocolsocketfactory (); // register the HTTPS protocol object you just created New Protocol ("https", Fcty, 443=  new//httpsclient usage normal use   New  Postmethod (URL); Httpclient.executemethod (method);

Httpsclient automatically obtain certificates without certificate access

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.