Android 链接https出现 javax.net.ssl.SSLException: hostname in certificate didn't match:

转载地址:http://blog.csdn.net/trbbadboy/article/details/11562511

抛出的错误是这样的:

[plain] view plain copy print ?
  1. javax.net.ssl.SSLException: hostname in certificate didn't match: <test.rigbee.cn> != <rigbee.cn> OR <rigbee.cn> OR <www.rigbee.cn>  
  2.     at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:185)  
  3.     at org.apache.http.conn.ssl.BrowserCompatHostnameVerifier.verify(BrowserCompatHostnameVerifier.java:54)  
  4.     at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:114)  
  5.     at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:95)  
  6.     at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:381)  
  7.     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:165)  
  8.     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)  
  9.     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)  
  10.     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)  
  11.     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)  
  12.     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)  
  13.     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)  

尝试过设置HttpsURLConnection.setDefaultHostnameVerifier(),不成功,估计是org.apache.http有自己的一套SSL东西,修改org.apache.http的主机名验证解决问题。

增加则用的一行:

[java] view plain copy print ?
  1. SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier());  

同时import一下:
[java] view plain copy print ?
  1. import org.apache.http.conn.ssl.AllowAllHostnameVerifier;  
  2. import org.apache.http.conn.ssl.SSLSocketFactory;  

如果你使用org.apache.http的话上面的就够了。


另外如果你使用原始的URLConnect的话请这样设置一下:

[java] view plain copy print ?
  1. try {         
  2.     SSLContext sc = SSLContext.getInstance("TLS");  
  3.     sc.init(nullnew TrustManager[] { new X509TrustManager() {  
  4.         @Override  
  5.         public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {  
  6.         }  
  7.   
  8.   
  9.         @Override  
  10.         public void checkServerTrusted(X509Certificate[] chain, String authType)  
  11.   
  12.   
  13.         throws CertificateException {  
  14.         }  
  15.   
  16.   
  17.         @Override  
  18.         public X509Certificate[] getAcceptedIssuers() {  
  19.             return null;  
  20.         }  
  21.     } }, new SecureRandom());  
  22.     HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());  
  23.     HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {  
  24.         @Override  
  25.         public boolean verify(String arg0, SSLSession arg1) {  
  26.             return true;  
  27.         }  
  28.     });  
  29. catch (Exception e) {  
  30.     e.printStackTrace();  
  31. }  


之后就可以使用了:

[java] view plain copy print ?
  1. String https = "https://www.google.com.hk";  
  2. try {  
  3.     HttpsURLConnection conn = (HttpsURLConnection) new URL(https).openConnection();  
  4.     conn.setDoOutput(true);  
  5.     conn.setDoInput(true);  
  6.     conn.connect();  
  7.     BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));  
  8.     StringBuffer sb = new StringBuffer();  
  9.     String line;  
  10.     while ((line = br.readLine()) != null)  
  11.         sb.append(line);  
  12.     Log(sb.toString());  
  13.   
  14.   
  15. catch (Exception e) {  
  16.     e.printStackTrace();  
  17. }

你可能感兴趣的:(Android 链接https出现 javax.net.ssl.SSLException: hostname in certificate didn't match:)