HTTPS Request for Volley

Source: Internet
Author: User

Prerequisite: Volley framework jar, server SSL certificate file (CRT,CET,PEM format, etc.)

If there is no volley, we can clone it through Git. Address:

Https://android.googlesource.com/platform/frameworks/volley

Let's take a look at the demo source provided by volley:

Git://github.com/ogrebgr/android_volley_examples.git

Copy some of the generic classes under Toolsbox in the demo source to your project, including:

Sslhttpclient.java,sslsocketfactory.java,ssx509trustmanager.java,exthttpclientstack.java

Also copy the Khandroid-httpclient-4.2.3.jar under the Libs directory to the Libs of your project and reference

The demo source contains an example of HTTPS, which is very simple:

ReplaceR.raw.test with your keystore inputstream keystore =getresources (). Openrawresource (r.raw.test);//usually Getting the request Queueshall be in singleton like in {@see act_simplerequest}//current approach are used just for Brevit Yrequestqueue queue = Volley        . Newrequestqueue (Act_sssslhttpclient.this,                         new Exthttpclientstack (new Sslhttpclient (KeyStore, "test123", 44401))); Stringrequest myreq = new Stringrequest (Method.get,                                        "https://ave.bolyartech.com:44401/https_test.html",                                       Createmyreqsuccesslistener (),                                       Createmyreqerrorlistener ());


As you can see in this code, you need an encrypted certificate (a BKS file in the demo's Raw directory) and this encrypted password, as well as the port number of the HTTPS provided by the server.

Let's look at how to generate this encrypted certificate file.

1. First, a cryptographic tool bouncy Castle is required:

Http://www.bouncycastle.org/latest_releases.html

Under Signed JAR files, select the corresponding jar package to download, corresponding to the version of the JDK installed on your computer. Here, I'm using Bcprov-jdk15on-152.jar.

2. For convenience, place the SSL certificate file on the server and the jar package in the same directory, open the terminal (open cmd under Windows), and enter the following code:

Keytool-importcert-v-trustcacerts-file "SERVER_CERT.CRT (server SSL certificate file name + suffix name)"-alias your app package name-keystore " My.bks (target generated bks file, file name random, suffix named bks) "-provider Org.bouncycastle.jce.provider.bouncycastleprovider-providerpath" Bcprov-jdk15on-152.jar "-storetype BKS

After a while the terminal will prompt to enter a password, this is the password of the encryption certificate, we need to use the code, remember this password, or re-operation again. Enter two password end, the terminal continues to ask, whether to trust this certificate, enter: Y, and then enter, the encryption certificate is completed.

3. Then put the generated my.bks in the raw directory of their own project, and then the volley demo HTTPS example code copied the past, modify the encryption certificate name and password can run. It is also important to note that the requested URL must be a domain name and not the IP address of the server.

But there is a problem, usually we test in the test environment, and the SSL certificate is bound to the domain name. This time we also need to use the simulator, modify the simulator hosts. The host of the emulator is located in the/system/etc directory. First pull the hosts out, and then we modify the file, for example, add a line 192.168.232.12 www.hicsg.com, and then save.

If this time directly to the modified hosts push back, there may be an out for memery error. At this point, we need to close the simulator and then open the terminal and enter:

EMULATOR-AVD Nexus_4_edited_api_17 (This is the name of the simulator)-partition-size 1024

At this point, the terminal will automatically start a new emulator, and according to memory is 1024MB to execute. Start successfully, also need to start a new terminal, enter:

Root Remount

Otherwise, there will be an error because there is no root permission.

The root is finished before the modified hosts can be push in. After push succeeds, you can debug the HTTPS request by running our own project.

Note: This modification of the simulator's hosts is only temporary, when the emulator is closed and restarted again, the emulator reverts to the default state, the hosts or the original.

Experiment proves that: for Android, if the server does not do the SSL certificate double authentication, in fact, no matter what you use the SSL certificate to encrypt all can run directly to their own servers HTTPS request.

Reference article:

http://ogrelab.ikratko.com/using-android-volley-with-self-signed-certificate/

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HTTPS Request for Volley

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.