如何忽略 Apache HttpClient 4.0 中的 SSL 证书错误

编程入门 行业动态 更新时间:2024-10-20 13:50:25
本文介绍了如何忽略 Apache HttpClient 4.0 中的 SSL 证书错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何使用 Apache HttpClient 4.0 绕过无效的 SSL 证书错误?>

How do I bypass invalid SSL certificate errors with Apache HttpClient 4.0?

推荐答案

您需要使用自己的 TrustManager 创建 SSLContext,并使用此上下文创建 HTTPS 方案.这是代码,

You need to create a SSLContext with your own TrustManager and create HTTPS scheme using this context. Here is the code,

SSLContext sslContext = SSLContext.getInstance("SSL"); // set up a TrustManager that trusts everything sslContext.init(null, new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { System.out.println("getAcceptedIssuers ============="); return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { System.out.println("checkClientTrusted ============="); } public void checkServerTrusted(X509Certificate[] certs, String authType) { System.out.println("checkServerTrusted ============="); } } }, new SecureRandom()); SSLSocketFactory sf = new SSLSocketFactory(sslContext); Scheme httpsScheme = new Scheme("https", 443, sf); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(httpsScheme); // apache HttpClient version >4.2 should use BasicClientConnectionManager ClientConnectionManager cm = new SingleClientConnManager(schemeRegistry); HttpClient httpClient = new DefaultHttpClient(cm);

更多推荐

如何忽略 Apache HttpClient 4.0 中的 SSL 证书错误

本文发布于:2023-11-07 22:56:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567668.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:证书   错误   HttpClient   Apache   SSL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!