Android 上的 httpclient ssl 证书
我在 Android 上使用 httpclient 时遇到一些 ssl 问题,我正在尝试详细访问自签名证书,我希望我的应用程序信任所有证书(我将仅使用 ssl 进行数据加密)。首先我尝试使用本指南 http://hc.apache.org/httpclient- 3.x/sslguide.html 在桌面上工作正常,但在 Android 上我仍然得到 javax.net.ssl.SSLException: 不受信任的服务器证书。在谷歌搜索后,我发现了一些其他示例如何启用 ssl。
http://groups.google.com/group/android-developers/ browser_thread/thread/62d856cdcfa9f16e - 当我使用 URLConnection 但使用 HttpClient 时仍然出现异常。
http://www.discursive.com/ books/cjcook/reference/http-webdav-sect-self-signed.html - 在桌面上使用 apache 的 jar 可以正常工作,但在 android 中使用 SDK 类中包含的文件无法使其工作。
所以有什么想法我如何信任所有证书在 Android 上使用 HttpClient
I have some troubles with ssl using httpclient on android i am trying to access self signed certificate in details i want my app to trust all certificates ( i will use ssl only for data encryption). First i tried using this guide http://hc.apache.org/httpclient-3.x/sslguide.html on Desktop is working fine but on android i still got javax.net.ssl.SSLException: Not trusted server certificate. After searching in google i found some other examples how to enable ssl.
http://groups.google.com/group/android-developers/browse_thread/thread/62d856cdcfa9f16e - Working when i use URLConnection but with HttpClient still got the exception.
http://www.discursive.com/books/cjcook/reference/http-webdav-sect-self-signed.html - on Desktop using jars from apache is working but in android using included in SDK classes can't make it work.
http://mail-archives.apache.org/mod_mbox/hc-httpclient-users/200808.mbox/%3C1218824624.6561.14.camel@ubuntu%3E - also get the same exception
So any ideas how can i trust all certificates on android using HttpClient
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您碰巧查看了 DefaultHttpClient 的代码,它看起来像这样:
注意 https 方案到 org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory() 的映射。
您可以为
org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory
接口创建自定义实现 (http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/protocol/SecureProtocolSocketFactory。 html),其中,您可以使用接受所有证书的自定义TrustManager
创建java.net.SSLSocket
。您可能需要在 中查看 JSSE 以了解更多详细信息http://java.sun.com/j2se/1.4.2/docs/guide/security/jsse/JSSERefGuide.html
If you happen to look at the code of DefaultHttpClient, it looks something like this:
Notice the mapping of https scheme to org.apache.http.conn.ssl.SSLSocketFactory.getSocketFactory().
You can create a custom implementation for
org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory
interface (http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/protocol/SecureProtocolSocketFactory.html) wherein, you can createjava.net.SSLSocket
with a customTrustManager
that accepts all certificate.You may want to look into JSSE for more details at http://java.sun.com/j2se/1.4.2/docs/guide/security/jsse/JSSERefGuide.html
关键思想是使用自定义的 SSLSocketFactory 实现 LayeredSocketFactory。定制的socket不需要HostNameVerifier。
然后,您可以继续在支持的方案注册表中使用自定义的 SSLSocketFactory。
The key idea is to use a customized SSLSocketFactory implementing LayeredSocketFactory. The customized socket doesn't need to HostNameVerifier.
You can then continue to use the customized SSLSocketFactory in the supported scheme registry.
我建议使用以下解决方案,而不是接受所有证书: 信任所有证书通过 HTTPS 使用 HttpClient
Rather than accepting all certificates, I recommend this solution: Trusting all certificates using HttpClient over HTTPS