HTTPS requests from the Google volley framework

Source: Internet
Author: User

First, insert a sentence. Google's volley framework itself is a support for HTTPS requests, but only for third-party organizations that have been certified.

Assuming that you are casually doing a certificate on the Internet, that volley is not supported by the request.


This article is about how to let volley support their own HTTPS certificate.


Change Volley source code: Com.android.myvolley.toolbox.HurlStack

   /**     * Create a {@link httpurlconnection} for the specified {@code URL}.     */    protected httpurlconnection createconnection (url url) throws IOException {        if (url.tostring (). Contains (" HTTPS ")) {            myhttpsmanager.allowallssl ();        }        Return (HttpURLConnection) url.openconnection ();    }
In the Hurlstack class of the CreateConnection method, do a filter processing:

if (Url.tostring (). Contains ("https")) {            myhttpsmanager.allowallssl ();        }
Direct Sticker code.

Import Java.security.keymanagementexception;import Java.security.nosuchalgorithmexception;import Java.security.securerandom;import Java.security.cert.certificateexception;import Java.security.cert.x509certificate;import Javax.net.ssl.hostnameverifier;import javax.net.ssl.HttpsURLConnection ; Import Javax.net.ssl.sslcontext;import Javax.net.ssl.sslsession;import Javax.net.ssl.trustmanager;import  Javax.net.ssl.x509trustmanager;public class Myhttpsmanager implements X509trustmanager {private static trustmanager[]    Trustmanagers;    @Override public void checkclienttrusted (x509certificate[] chain, String authtype) throws Certificateexception {}     @Override public void checkservertrusted (x509certificate[] chain, String authtype) throws Certificateexception {}    @Override public x509certificate[] Getacceptedissuers () {return new x509certificate[0]; } public static void Allowallssl () {Httpsurlconnection.setdefaulthostnameverifier (New Hostnameverifier ({@Override public boolean verify (String arg0, sslsession arg1) {//TODO Auto-gene            Rated method stub return true;        }        });        Sslcontext context = null;        if (trustmanagers = = null) {trustmanagers = new trustmanager[] {new Myhttpsmanager ()};            } try {context = Sslcontext.getinstance ("TLS");        Context.init (NULL, trustmanagers, New SecureRandom ());        } catch (NoSuchAlgorithmException e) {e.printstacktrace ();        } catch (Keymanagementexception e) {e.printstacktrace ();    } httpsurlconnection.setdefaultsslsocketfactory (context. Getsocketfactory ()); }}

then ask for the time to add a ghost ID in the head. Sign and so on, you can communicate with the server.

For example, volley rewrite the Getheaders method:

public void SendRequest (int method, String URL, jsonobject jsonrequest, Final String Sessiontoken, final onresponselistene        R listener,final Onerrorlistener errorlistener) {inituseragent (); Jsonobjectrequest jsonobjectrequest = new Jsonobjectrequest (method, URL, jsonrequest, new response.listener< Jsonobject> () {@Override public void Onresponse (Jsonobject response) {LISTENER.O            Nresponse (response); }}, new Response.errorlistener () {@Override public void Onerrorresponse (volleyerror error)            {errorlistener.onerrorresponse (Error);                }}) {@Override public map<string, String> getheaders () throws Authfailureerror {                hashmap<string, string> headers = new hashmap<string,string> ();                Headers.put ("Content-type", "Application/json");                Headers.put ("X-app-id", APPID); Headers.puT ("X-app-sign", sign);                Headers.put ("X-session-token", Sessiontoken);                Headers.put ("User-agent", useragent);            return headers;        }        };    Volley.newrequestqueue (context). Add (Jsonobjectrequest); }


HTTPS requests from the Google volley framework

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.