android上的httpclient ssl证书 [英] httpclient ssl certificate on android

查看:55
本文介绍了android上的httpclient ssl证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 ssl 在 android 上使用 httpclient 时遇到了一些问题我正在尝试详细访问自签名证书我希望我的应用程序信任所有证书(我将仅使用 ssl 进行数据加密).首先我尝试使用本指南 http://hc.apache.org/httpclient-桌面上的 3.x/sslguide.html 工作正常,但在 android 上我仍然得到 javax.net.ssl.SSLException: Not trust server certificate.在谷歌搜索后,我发现了一些其他如何启用 ssl 的示例.

http://groups.google.com/group/android-developers/browse_thread/thread/62d856cdcfa9f16e - 当我使用 URLConnection 但使用 HttpClient 时工作仍然出现异常.

http://www.discursive.com/books/cjcook/reference/http-webdav-sect-self-signed.html - 在桌面上使用来自 apache 的 jar 可以正常工作,但在 android 中使用 SDK 类中包含的无法使其工作.p>

http://mail-archives.apache.org/mod_mbox/hc-httpclient-users/200808.mbox/%3C1218824624.6561.14.camel@ubuntu%3E - 也得到同样的例外

所以任何想法我如何使用 HttpClient 信任 android 上的所有证书

解决方案

如果你碰巧看到 DefaultHttpClient 的代码是这样的:

 @Override受保护的 ClientConnectionManager createClientConnectionManager() {SchemeRegistry 注册表 = new SchemeRegistry();注册表.注册(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));注册表.注册(新方案(https",SSLSocketFactory.getSocketFactory(),443));ClientConnectionManager connManager = null;HttpParams 参数 = getParams();...}

注意 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.

您可能希望在 http://java.sun.com/j2se/1.4.2/docs/guide/security/jsse/JSSERefGuide.html

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

解决方案

If you happen to look at the code of DefaultHttpClient, it looks something like this:

   @Override
    protected ClientConnectionManager createClientConnectionManager() {
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(
                new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(
                new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

        ClientConnectionManager connManager = null;     
        HttpParams params = getParams();
        ...
    }

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 create java.net.SSLSocket with a custom TrustManager 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

这篇关于android上的httpclient ssl证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆