码迷,mamicode.com
首页 > 编程语言 > 详细

javax.net.ssl.SSLException: hostname in certificate didn't match:

时间:2015-07-03 10:43:50      阅读:1034      评论:0      收藏:0      [点我收藏+]

标签:exceptio   ssl   网络请求   android   https   

 

每个协议有自己的一套SSL东西,Android 链接https经常会抛出如下错误

 javax.net.ssl.SSLException: hostname in certificate didn't match: <test.rigbee.cn> != <rigbee.cn> OR <rigbee.cn> OR <www.rigbee.cn>
 	at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:185)
 	at org.apache.http.conn.ssl.BrowserCompatHostnameVerifier.verify(BrowserCompatHostnameVerifier.java:54)
 	at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:114)
 	at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:95)
 	at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:381)
 	at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:165)
 	at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
 	at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
 	at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
 	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
 	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
 	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)

如果使用org.apache.http,修改主机名验证解决问题

import org.apache.http.conn.ssl.AllowAllHostnameVerifier;  
import org.apache.http.conn.ssl.SSLSocketFactory;  
SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier()); 

如果使用原生URLConnect的话添加如下设置

try {         
    SSLContext sc = SSLContext.getInstance("TLS");  
    sc.init(null, new TrustManager[] { new X509TrustManager() {  
        @Override  
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {  
        }  
  
  
        @Override  
        public void checkServerTrusted(X509Certificate[] chain, String authType)  
  
  
        throws CertificateException {  
        }  
  
  
        @Override  
        public X509Certificate[] getAcceptedIssuers() {  
            return null;  
        }  
    } }, new SecureRandom());  
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());  
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {  
        @Override  
        public boolean verify(String arg0, SSLSession arg1) {  
            return true;  
        }  
    });  
} catch (Exception e) {  
    e.printStackTrace();  
}  


版权声明:本文为博主原创文章,未经博主允许不得转载。

javax.net.ssl.SSLException: hostname in certificate didn't match:

标签:exceptio   ssl   网络请求   android   https   

原文地址:http://blog.csdn.net/u013771273/article/details/46733503

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!