Volley HTTPS trusts all certificate implementations:
Public classHttpstrustmanagerImplementsX509trustmanager {Private Statictrustmanager[] trustmanagers; Private Static Finalx509certificate[] _acceptedissuers =Newx509certificate[]{}; @Override Public voidcheckclienttrusted (java.security.cert.x509certificate[] x509certificates, String s)throwsjava.security.cert.CertificateException {} @Override Public voidcheckservertrusted (java.security.cert.x509certificate[] x509certificates, String s)throwsjava.security.cert.CertificateException {} Public Booleanisclienttrusted (x509certificate[] chain) {return true; } Public Booleanisservertrusted (x509certificate[] chain) {return true; } @Override Publicx509certificate[] Getacceptedissuers () {return_acceptedissuers; } Public Static voidAllowallssl () {Httpsurlconnection.setdefaulthostnameverifier (NewHostnameverifier () {@Override Public BooleanVerify (String arg0, sslsession arg1) {return true; } }); Sslcontext Context=NULL; if(Trustmanagers = =NULL) {trustmanagers=Newtrustmanager[]{NewHttpstrustmanager ()}; } Try{Context= Sslcontext.getinstance ("TLS"); Context.init (NULL, Trustmanagers,Newsecurerandom ()); } Catch(nosuchalgorithmexception e) {e.printstacktrace (); } Catch(keymanagementexception e) {e.printstacktrace (); } httpsurlconnection.setdefaultsslsocketfactory (context. Getsocketfactory ()); }}
The code calls the following method:
Httpstrustmanager.allowallssl ();//mainly this line implements the operation to trust all certificates String tag_string_req= "String_req"; Stringrequest strreq=Newstringrequest (Request.Method.POST, Your_https_url,NewResponse.listener<string>() {@Override Public voidOnresponse (String response) {LOG.D (TAG,"Response:" +response); }}, NewResponse.errorlistener () {@Override Public voidonerrorresponse (volleyerror error) {VOLLEYLOG.D (TAG,"Error:" +error.getmessage ()); }}) {@OverrideprotectedMap<string, string>Getparams () {Map<string, string> params =NewHashmap<string, string>(); Params.put ("Username", "Max"); Params.put ("Password", "123456"); returnparams; }}; Appcontroller.getinstance (). Addtorequestqueue (Strreq, tag_string_req);
Trust the specified certificate file :
The Hurlstack code contains the following methods:
/** @param urlrewriter Rewriter to use for request URLs @param Sslsocketfactory SSL factory to use for HTTPS connections * Hurlstack The construction of this class you will find that volley can actually support HTTPS, also located under the Toolbox package c9/>* /Public Hurlstack (Urlrewriter Urlrewriter, sslsocketfactory Sslsocketfactory) { = urlrewriter; = sslsocketfactory; }
/*** Opens an {@linkhttpurlconnection} with parameters. * @paramURL *@returnAn open connection *@throwsIOException*/ Privatehttpurlconnection openconnection (URL url, request<?> Request)throwsIOException {httpurlconnection connection=createconnection (URL); intTimeoutms =Request.gettimeoutms (); Connection.setconnecttimeout (TIMEOUTMS); Connection.setreadtimeout (TIMEOUTMS); Connection.setusecaches (false); Connection.setdoinput (true); //Use caller-provided custom sslsocketfactory, if any, for HTTPS if("https". Equals (Url.getprotocol ()) && msslsocketfactory! =NULL{(httpsurlconnection) connection). Setsslsocketfactory (Msslsocketfactory); } returnconnection; }
In the case of HTTPS protocol, it is guaranteed that msslsocketfactory is not NULL, so the main problem is to pass in sslsocketfactory this parameter.
StackOverflow above is a tool class that produces this content:
Privatetrustmanager[] Getwrappedtrustmanagers (trustmanager[] trustmanagers) {FinalX509trustmanager Originaltrustmanager = (x509trustmanager) trustmanagers[0]; return Newtrustmanager[]{NewX509trustmanager () { Publicx509certificate[] Getacceptedissuers () {returnoriginaltrustmanager.getacceptedissuers (); } Public voidcheckclienttrusted (x509certificate[] certs, String authtype) {Try{originaltrustmanager.checkclienttrusted (certs, authtype); } Catch(certificateexception e) {e.printstacktrace (); } } Public voidcheckservertrusted (x509certificate[] certs, String authtype) {Try{originaltrustmanager.checkservertrusted (certs, authtype); } Catch(certificateexception e) {e.printstacktrace (); } } } }; }PrivateSslsocketfactory getsslsocketfactory_certificate (String keystoretype,intkeystoreresid)throwscertificateexception, Keystoreexception, IOException, NoSuchAlgorithmException, keymanagementexception { Certificatefactory CF= Certificatefactory.getinstance ("the"); InputStream Cainput=getresources (). Openrawresource (KEYSTORERESID); Certificate CA=cf.generatecertificate (cainput); Cainput.close (); if(Keystoretype = =NULL|| Keystoretype.length () = = 0) {Keystoretype=Keystore.getdefaulttype (); } KeyStore KeyStore=keystore.getinstance (Keystoretype); Keystore.load (NULL,NULL); Keystore.setcertificateentry ("Ca", CA); String Tmfalgorithm=Trustmanagerfactory.getdefaultalgorithm (); Trustmanagerfactory TMF=trustmanagerfactory.getinstance (Tmfalgorithm); Tmf.init (KeyStore); Trustmanager[] Wrappedtrustmanagers=getwrappedtrustmanagers (Tmf.gettrustmanagers ()); Sslcontext Sslcontext= Sslcontext.getinstance ("TLS"); Sslcontext.init (NULL, Wrappedtrustmanagers,NULL); returnsslcontext.getsocketfactory ();}PrivateSslsocketfactory Getsslsocketfactory_keystore (String keystoretype,intKeystoreresid, String Keypassword)throwscertificateexception, Keystoreexception, IOException, NoSuchAlgorithmException, keymanagementexception {Input Stream Cainput=getresources (). Openrawresource (KEYSTORERESID); //creating a KeyStore containing trusted CAs if(Keystoretype = =NULL|| Keystoretype.length () = = 0) {Keystoretype=Keystore.getdefaulttype (); } KeyStore KeyStore=keystore.getinstance (Keystoretype); Keystore.load (Cainput, Keypassword.tochararray ()); //creating a TrustManager that trusts the CAs in the KeyStoreString Tmfalgorithm=Trustmanagerfactory.getdefaultalgorithm (); Trustmanagerfactory TMF=trustmanagerfactory.getinstance (Tmfalgorithm); Tmf.init (KeyStore); Trustmanager[] Wrappedtrustmanagers=getwrappedtrustmanagers (Tmf.gettrustmanagers ()); Sslcontext Sslcontext= Sslcontext.getinstance ("TLS"); Sslcontext.init (NULL, Wrappedtrustmanagers,NULL); returnsslcontext.getsocketfactory (); }
Call the next two, you can get sslsocketfactory content, using the following:
Sslsocketfactory sslsocketfactory = Getsslsocketfactory_keystore ("BKS", R.raw.androidbksv1, "123456789"= Getsslsocketfactory_certificate ("BKS", R.raw.androidbksv1_cert);
Http://stackoverflow.com/questions/32154115/android-volley-self-signed-https-trust-anchor-for-certification-path-not-found
"Sixth article" volley HTTPS related