Volley adding HTTPS support

Source: Internet
Author: User

Volley support HTTPS, but the framework is not added to the default, you can modify a small number of source code to achieve;

One is to override the third construct of the Volley.java Newrequestqueueindisk method call. And because these three constructs last called the one with the most parameters, the Sslsocketfactory instance can also be generated directly by default in the third construct.

The second is to add the Httpstrustmanager class in Toolbox (on the Code) and make minor changes to the Hurlstack Createconnetcion method.

 PackageCom.android.volley.toolbox;Importjava.security.KeyManagementException;Importjava.security.NoSuchAlgorithmException;ImportJava.security.SecureRandom;Importjava.security.cert.X509Certificate;ImportJavax.net.ssl.HostnameVerifier;Importjavax.net.ssl.HttpsURLConnection;ImportJavax.net.ssl.SSLContext;Importjavax.net.ssl.SSLSession;ImportJavax.net.ssl.TrustManager;ImportJavax.net.ssl.X509TrustManager; Public classHttpstrustmanagerImplementsX509trustmanager {Private Statictrustmanager[] trustmanagers; Private Static Finalx509certificate[] _acceptedissuers =Newx509certificate[] {}; @Override Public voidcheckclienttrusted (java.security.cert.x509certificate[] x509certificates, String s)throwsjava.security.cert.CertificateException {//To change body of implemented methods use File | Settings | File//Templates.} @Override Public voidcheckservertrusted (java.security.cert.x509certificate[] x509certificates, String s)throwsjava.security.cert.CertificateException {//To change body of implemented methods use File | Settings | File//Templates.    }     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) {//TODO auto-generated Method Stub                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 ()); }}

protected throws IOException {        if("https". Equals (Url.getprotocol (). toLowerCase ())) {            Httpstrustmanager.allowallowssl ();        }         return (HttpURLConnection) url.openconnection ();}

Third, each time volleyqueue before the Httpstrustmanager.allowallowssl ();

Httpstrustmanager.allowallowssl (); Mvolleyquene.add (Newrequest ());

The default is in the second type.

Volley adding HTTPS support

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.