The program has an exception like this: Javax.net.ssl.SSLException:hostname in certificate didn ' t match. The exception occurs on this line of code: Httpclient.execute (HttpGet);
Preliminary judgment is the issue of the certificate, prepared to use the method of ignoring the certificate to solve, but in the internet for half a day did not find a reliable code to solve, and finally the solution.
To get a lesson, the Internet to find the answer is not reliable, because the specific problems need to be specific analysis, other people's methods may not be able to solve your, must think for themselves, pondering. The most important thing on the net is to find the solution to the problem, and finally to think about the specific code to solve their own problems.
Finally spit groove A sentence, the online solution is mostly nonsense a basket, half a day said not to the core solution.
Core idea: Inheriting sslsocketfactory, overriding a validation certificate always returns true for Unverifysocketfactory,
* New One such Unverifysocketfactory object is set to HttpClient. The code is as follows:
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.sslexception;import Javax.net.ssl.sslsession;import Javax.net.ssl.sslsocket;import Javax.net.ssl.trustmanager;import Javax.net.ssl.x509trustmanager;import Org.apache.http.httpversion;import Org.apache.http.client.httpclient;import Org.apache.http.conn.clientconnectionmanager;import Org.apache.http.conn.scheme.plainsocketfactory;import Org.apache.http.conn.scheme.scheme;import Org.apache.http.conn.scheme.schemeregistry;import Org.apache.http.conn.ssl.sslsocketfactory;import Org.apache.http.conn.ssl.x509hostnameverifier;import Org.apache.http.impl.client.DefaulThttpclient;import Org.apache.http.impl.conn.tsccm.threadsafeclientconnmanager;import Org.apache.http.params.basichttpparams;import Org.apache.http.params.httpparams;import Org.apache.http.params.httpprotocolparams;import Org.apache.http.protocol.http;import android.util.Log;/** * Tool class: Create a httpclient instance that ignores user certificate validation * @date 2015-02-13 * @author Barry */public class Certificatevalidationignored {public stat IC HttpClient getnocertificatehttpclient (String URL) {return getcertificatevalidationignoredhttpclient ();} private static HttpClient getcertificatevalidationignoredhttpclient () {try {KeyStore Truststore = Keystore.getinstance (KeyStore. Getdefaulttype ()); Truststore.load (null, NULL); The core code that creates a Unverifysocketfactory object, validates that the certificate always returns true sslsocketfactory SF = new Unverifysocketfactory (Truststore); 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) {log.d ("certificatevalidationignored", "Create a HttpClient object that ignores user certificates failed, attempt to create a normal httpclient object"); E.printstacktrace (); return new Defaulthttpclient (); }}/** * Core class * Unverifysocketfactory: A validation certificate always returns true for Sslsocketfactory subclasses */private static X509hostnameverifier Igno Reverifier;private Static class Unverifysocketfactory extends Sslsocketfactory {sslcontext Sslcontext = Sslcontext.getinstance ("TLS");p ublic unverifysocketfactory (KeyStore Truststore) throws NoSuchAlgorithmException, Keymanagementexception,keystoreexception, unrecoverablekeyexception { Super (Truststore); TrustManager TM = new X509trustmanager () {public void checkclienttrusted (x509certificate[] chain,string authtype) throws certificateexception {}public void checkservertrusted (x509certificate[] chain,string authtype) throws certificateexception {}public x509certificate[] getacceptedissuers () {return null;}}; Sslcontext.init (NULL, new trustmanager[] {TM}, null);} @Overridepublic socket Createsocket (socket socket, String host, int Port,boolean autoClose) throws IOException, Unknownho stexception {return sslcontext.getsocketfactory (). Createsocket (socket, host,port, autoClose);} Core code @overridepublic void Sethostnameverifier (X509hostnameverifier hostnameverifier) {//TODO auto-generated method Stubignoreverifier = new X509hostnameverifier () {@Overridepublic void verify (String arg0, string[] arg1, string[] arg2) th Rows sslexception {} @Overridepublic void verify (STRing arg0, x509certificate arg1) throws sslexception {} @Overridepublic void verify (String arg0, Sslsocket arg1) throws Ioexc eption {}//Most core code @overridepublic Boolean verify (String arg0, sslsession arg1) {return true;}}; Super.sethostnameverifier (ignoreverifier);} @Overridepublic x509hostnameverifier Gethostnameverifier () {return ignoreverifier;} @Overridepublic Socket Createsocket () throws IOException {return sslcontext.getsocketfactory (). Createsocket ();}}
HttpClient methods for ignoring user certificate validation