HTTPS访问站点,出现证书问题解决(转载)
2010-01-16 17:48
423 查看
用URLConnection获取一个Https站点的东西 老是抛出异常说证书认证不过 不过对服务迁移性的考虑 又不想导入证书到JRE里面(总不会说出现紧急情况 需要搬迁到其他服务器 还要我记得去导入证书吧) 最奇怪的是 Windows下运行的好好的 没有什么异常 一去Linux就出错
最后没有办法 只有 强制绕过这个检查了
就是要检查的时候 让他直接返回true 通过验证就OK了
在建立连接的时候 还要设置一下这个管理器
最后没有办法 只有 强制绕过这个检查了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.X509TrustManager; public class TrustAnyTrustManager implements X509TrustManager { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[] {}; } } |
1 2 3 4 5 6 7 8 9 | import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLSession; public class TrustAnyHostnameVerifier implements HostnameVerifier { public boolean verify(String hostname, SSLSession session) { // 直接Pass return true; } } |
在建立连接的时候 还要设置一下这个管理器
1 2 3 4 5 6 7 8 9 | SSLContext context = SSLContext.getInstance("SSL"); context.init(null,new TrustManager[] { new TrustAnyTrustManager() }, new SecureRandom()); // 获取URL 进行连接 URL url = new URL(urlString); connection = (HttpsURLConnection) url.openConnection(); connection.setSSLSocketFactory(context.getSocketFactory()); connection.setHostnameVerifier(new TrustAnyHostnameVerifier()); connection.connect(); |
相关文章推荐
- HTTPS访问站点,出现证书问题解决
- HttpClient访问https站点证书加载问题
- 转载-解决使用httpClient 4.3.x登陆 https时的证书报错问题
- 解决linux下https访问证书问题
- 关于如何取消访问https时的提示:“此网站的安全证书存在问题”的解决方法
- 解决linux下访问https站点问题
- IE内核浏览器访问https网站出现证书问题
- 访问本地站点出现的一些问题及解决方法
- 解决HttpClient访问https(包括通过http代理)出现unable to find valid certification path to requested target的问题
- 微信支付证书问题C#使用Https访问加载SSL证书本地调试正常服务器IIS内部错误解决方法
- 解决因HTTPS 证书出错访问不了网页的问题(GitHub为例)
- 解决chrome访问https网站出现“您的连接不是私密的问题”
- BlueCoat SG设备对非标准HTTPS应用访问提示不可信任发行者证书问题解决
- Failed to fetch URl https://dl-ssl.google.com/android/repository/addo Android SDK更新以及ADT更新出现问题的解决办法
- Charles 映射https为http 解决抓包证书问题
- curl 客户端证书 访问https站点
- AFNetworking 原作者都无法解决的问题: 如何使用ip直接访问https网站?
- 解决Wampserver在win8下出现403禁止访问问题
- 项目调试时候,出现其中用到的一个组件“访问被拒绝”的解决方法。(转载)