Android 上的自签名 SSL 接受 [英] Self-signed SSL acceptance on Android

查看:27
本文介绍了Android 上的自签名 SSL 接受的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在 Android 上接受 Java 中的自签名证书?

How do I accept a self-signed certificate in Java on Android?

代码示例将是完美的.

我在互联网上到处寻找,虽然有些人声称找到了解决方案,但它要么不起作用,要么没有示例代码来支持它.

I've looked everywhere on the Internet and while some people claim to have found the solution, it either does not work or there is no sample code to back it up.

推荐答案

我在 exchangeIt 中有这个功能,它通过 WebDav 连接到 Microsoft Exchange.这是创建 HttpClient 的一些代码,它将通过 SSL 连接到自签名证书:

I have this functionality in exchangeIt, which connects to Microsoft exchange via WebDav. Here's some code to create an HttpClient which will connect to self signed cert's via SSL:

SchemeRegistry schemeRegistry = new SchemeRegistry();
// http scheme
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
// https scheme
schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));

HttpParams params = new BasicHttpParams();
params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);

EasySSLSocketFactory 是 此处,EasyX509TrustManager 是 此处.

The EasySSLSocketFactory is here, and the EasyX509TrustManager is here.

exchange 的代码是开源的,托管在 googlecode 这里,如果你有任何问题.我不再积极研究它,但代码应该可以工作.

The code for exchangeIt is open source, and hosted on googlecode here, if you have any issues. I'm not actively working on it anymore, but the code should work.

请注意,从 Android 2.2 开始,流程发生了一些变化,因此请查看 this 使上面的代码工作.

Note that since Android 2.2 the process has changed a bit, so check this to make the code above work.

这篇关于Android 上的自签名 SSL 接受的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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