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