Android webview & httpclient processes SSL (https) Verification

Source: Internet
Author: User
Tags blank page

1. webview SSL Verification

@ Override
Public void onreceivedsslerror (webview view, sslerrorhandler handler,
Sslerror error ){
Super. onreceivedsslerror (view, handler, error );

// Handler. Cancel (); the default processing method. The webview becomes a blank page.

// Handlemessage (Message MSG); other processing

Handler. Proceed ();

}

2. httpclient SSL Verification

Private Static httpclient getnewhttpclient (){
// Return an httpclient configured to accept all SSL certificates
Try {
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", mysslsocketfactory
. Getsslsocketfactory (), 443 ));

Clientconnectionmanager CCM = new threadsafeclientconnmanager (
Params, registry );

// Httpclient = new
// Org. Apache. http. impl. Client. contentencodinghttpclient (CCM,
// Params); // throws eofexception in 4.1.1 (fixed in 4.1.2 ?)
Defaulthttpclient httpclient = new defaulthttpclient (CCM, Params );

// Httpclient. addresponseinterceptor (New
// Gziphttpresponseinterceptor ());

Return httpclient;
} Catch (ioexception e ){
Apputils. logd ("couldnt set up SSL properly ");
Return new defaulthttpclient ();
}
}

Mysslsocketfactory

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 java. Security. cert. certificateexception;
Import java. Security. cert. x509certificate;

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

Import org. Apache. http. Conn. SSL. sslsocketfactory;
Public class mysslsocketfactory extends sslsocketfactory
{
Private Static mysslsocketfactory instance;

Private Final sslcontext = sslcontext. getinstance ("TLS ");

Public static mysslsocketfactory getsslsocketfactory () throws ioexception
{
If (instance = NULL)
{
Try
{
Keystore truststore = keystore. getinstance (keystore. getdefaulttype ());
Truststore. Load (null, null );
Instance = new mysslsocketfactory (truststore );
Instance. sethostnameverifier (sslsocketfactory. allow_all_hostname_verifier );
}
Catch (certificateexception E)
{
Throw new ioexception ("couldnt set up SSL properly", e );
}
Catch (keystoreexception E)
{
Throw new ioexception ("couldnt set up SSL properly", e );
}
Catch (nosuchalgorithmexception E)
{
Throw new ioexception ("couldnt set up SSL properly", e );
}
Catch (keymanagementexception E)
{
Throw new ioexception ("couldnt set up SSL properly", e );
}
Catch (unrecoverablekeyexception E)
{
Throw new ioexception ("couldnt set up SSL properly", e );
}
}
Return instance;
}

Private mysslsocketfactory (keystore truststore) throws nosuchalgorithmexception, keymanagementexception, keystoreexception, unrecoverablekeyexception
{
Super (truststore );

Trustmanager TM = new x509trustmanager ()
{
Public void checkclienttrusted (x509certificate [] Chain, string authtype) throws certificateexception
{
// Accept
}

Public void checkservertrusted (x509certificate [] Chain, string authtype) throws certificateexception
{
// Accept
}

Public x509certificate [] getacceptedissuers ()
{
Return NULL;
}
};

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 ();
}
}

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.