Javax.net.ssl.SSLPeerUnverifiedException approach to Android development

Source: Internet
Author: User

According to the relevant information to solve the following:
1, the preparation of Sslsocketfactoryex to replace the original sslsocketfactory,

The code is as follows:

The code is as follows Copy Code

Package Com.nbcio.baishicha.yunbiji;

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

}

}

2, write a new httpclient getnewhttpclient to replace the original defaulthttpclient, the code is as follows:
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 ();
}
}

3, in the Postrequest call when the following methods can be:

The code is as follows Copy Code

HttpResponse HttpResponse = (HttpResponse) getnewhttpclient (). Execute (Postmethod);
Related Posts:

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.