Android httpclient access https

Source: Internet
Author: User

I recently wrote a small app on Android to visit the new academic affairs office of the school. I did not expect that the URI is https: // type, and the httpclient seems to be invalid. After a day, Baidu finally found a solution. As follows:

First, write a custom class to inherit sslsocketfactory.

Import java. Io. ioexception;
Import java.net. Socket;
Import java.net. unknownhostexception;
Import java. Security. keymanagementexception;
Import java. Security. keystore;
Import java. Security. keystoreexception;
Import java. Security. nosuchalgorithmexception;
Import java. Security. unrecoverablekeyexception;

Import javax.net. SSL. sslcontext;
Import javax.net. SSL. trustmanager;
Import javax.net. SSL. x509trustmanager;

Import org. Apache. http. Conn. SSL. sslsocketfactory;

Public class sslsocketfactoryex extends sslsocketfactory {

Sslcontext = sslcontext. getinstance ("TLS ");

Public sslsocketfactoryex (keystore truststore)
Throws nosuchalgorithmexception, keymanagementexception,
Keystoreexception, unrecoverablekeyexception {
Super (truststore );

Trustmanager TM = new x509trustmanager (){
Public java. Security. cert. x509certificate [] getacceptedissuers () {return NULL ;}

@ Override
Public void checkclienttrusted (
Java. Security. cert. x509certificate [] Chain, string authtype)
Throws java. Security. cert. certificateexception {}

@ Override
Public void checkservertrusted (
Java. Security. cert. x509certificate [] Chain, string authtype)
Throws java. Security. cert. certificateexception {}
};
Sslcontext. INIT (null, new trustmanager [] {TM}, null );
}

@ Override
Public socket createsocket (Socket socket, string host, int port, Boolean autoclose) throws ioexception, unknownhostexception {
Return sslcontext. getsocketfactory (). createsocket (socket, host, port, autoclose );
}

@ Override
Public socket createsocket () throws ioexception {
Return sslcontext. getsocketfactory (). createsocket ();
}
}


If you use the httpclient returned in the following method to send a get or POST request, you can access the https: // type normally. Of course, you can also access the http: // type normally.

Public static httpclient getnewhttpclient (){
Try {
Keystore truststore = keystore. getinstance (keystore. getdefaulttype ());
Truststore. Load (null, null );

Sslsocketfactory Sf = new sslsocketfactoryex (truststore );
SF. sethostnameverifier (sslsocketfactory. allow_all_hostname_verifier );

Httpparams Params = new basichttpparams ();
Httpprotocolparams. setversion (Params, httpversion. http_1_1 );
Httpprotocolparams. setcontentcharset (Params, HTTP. utf_8 );

Schemeregistry registry = new schemeregistry ();
Registry. Register (New Scheme ("HTTP", plainsocketfactory. getsocketfactory (), 80 ));
Registry. Register (New Scheme ("HTTPS", SF, 443 ));

Clientconnectionmanager CCM = new threadsafeclientconnmanager (Params, registry );

Return new defaulthttpclient (CCM, Params );
} Catch (exception e ){
Return new defaulthttpclient ();
}
}


From: http://www.eoeandroid.com/thread-161747-1-1.html

Related Article

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.