SecureProtocolSocketFactory

SecureProtocolSocketFactory

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

问题描述

我有一些麻烦,ssl使用httpclient在Android上我想尝试访问自签名证书的详细信息,我想我的应用程序信任所有证书(我将使用ssl只为数据加密)。首先,我尝试使用本指南在桌面上工作正常,但在Android我仍然有javax.net.ssl.SSLException:不受信任的服务器证书。在google中搜索后,我发现了一些其他的例子如何启用ssl。



- 工作时,当我使用URLConnection但与HttpClient仍然得到例外。



- 在桌面上使用jar从apache是​​工作,但在android使用SDK类中包含的内容不能使其工作。



- 也得到相同的异常



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

解决方案

如果你碰巧看看DefaultHttpClient的代码,它看起来像这样:

  @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();
...
}

注意https方案到org的映射。 apache.http.conn.ssl.SSLSocketFactory.getSocketFactory()。



您可以为 org.apache.commons.httpclient创建自定义实现。 protocol.SecureProtocolSocketFactory 接口()其中,您可以创建 $ c> java.net.SSLSocket 使用接受所有证书的自定义 TrustManager



您可以访问


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

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

08-04 06:01