1. 程式人生 > >Android 連結https出現 javax.net.ssl.SSLException: hostname in certificate didn't match:

Android 連結https出現 javax.net.ssl.SSLException: hostname in certificate didn't match:

轉載地址:http://blog.csdn.net/trbbadboy/article/details/11562511

丟擲的錯誤是這樣的:

[plain] view plaincopyprint?
  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 plaincopyprint?
  1. SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier());  

同時import一下: [java] view plaincopyprint?
  1. import org.apache.http.conn.ssl.AllowAllHostnameVerifier;  
  2. import org.apache.http.conn.ssl.SSLSocketFactory;  

如果你使用org.apache.http的話上面的就夠了。

另外如果你使用原始的URLConnect的話請這樣設定一下:

[java] view plaincopyprint?
  1. try {         
  2.     SSLContext sc = SSLContext.getInstance("TLS");  
  3.     sc.init(nullnew TrustManager[] { new X509TrustManager() {  
  4.         @Override
  5.         publicvoid checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {  
  6.         }  
  7.         @Override
  8.         publicvoid checkServerTrusted(X509Certificate[] chain, String authType)  
  9.         throws CertificateException {  
  10.         }  
  11.         @Override
  12.         public X509Certificate[] getAcceptedIssuers() {  
  13.             returnnull;  
  14.         }  
  15.     } }, new SecureRandom());  
  16.     HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());  
  17.     HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {  
  18.         @Override
  19.         publicboolean verify(String arg0, SSLSession arg1) {  
  20.             returntrue;  
  21.         }  
  22.     });  
  23. catch (Exception e) {  
  24.     e.printStackTrace();  
  25. }  


之後就可以使用了:

[java] view plaincopyprint?
  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. catch (Exception e) {  
  14.     e.printStackTrace();  
  15. }