问题描述
在我正在开发的应用程序中,我必须与 Web 服务器建立 HTTPS 连接.我收到了证书不受信任的错误,在咨询了 stackoverflow 之后,我发现了这篇博文:http://blog.antoine.li/index.php/2010/10/android-trusting-ssl-certificates/
In the app I'm working on, I have to make an HTTPS connection to a web server. I was getting certificate not trusted errors and after consulting stackoverflow, I found this blog posting: http://blog.antoine.li/index.php/2010/10/android-trusting-ssl-certificates/
该服务器的 CA 似乎未包含在 Android 的默认商店中.简而言之,我下载了所有证书,使用 BKS 提供程序创建了一个密钥库,导入了密钥,将密钥库导入到我的项目中,继承了 DefaultHttpClient 类以强制它使用我的密钥库.
It seems like the CA for this server is not included in Android's default store. In a nutshell, I downloaded all the certificates, created a keystore with the BKS provider, imported the keys, imported the keystore into my project, subclassed the DefaultHttpClient class to force it to use my keystore.
按照博客中的步骤操作后,它在模拟器上完美运行.但是,当我在设备上对其进行测试时,它会间歇性地失败.我想我已经隔离了一个模式.似乎过了一段时间后,我尝试建立 HTTPS 连接,它会失败.然后,如果我再次尝试相同的连接,它将成功.如果我等待一段时间然后再试一次,它第一次失败,重复尝试成功.我可能可以通过多次尝试失败来解决它,但我想知道发生了什么.该行为暗示了某种缓存,但我不知道如何去寻找它或修改它的行为.有没有人对正在发生的事情有任何建议或知道我做错了什么?任何帮助将不胜感激.
After following the steps in the blog, it worked perfectly on the emulator. However, when I test it on a device, it fails intermittently. I think I've isolated a pattern. It seems like after some time has passed and I try to make an HTTPS connection, it will fail. Then, if I attempt the same connection again, it will succeed. If I wait a while and then try again, it fails the first time, succeeds on repeated attempts. I can probably fix it by just making multiple attempts on failure, but I would like to know what is going on. The behavior suggests some kind of cache but I don't know how to go about finding it or modifying its behavior. Does anyone have any suggestions about what is going on or know what I'm doing wrong? Any help would be appreciated.
推荐答案
我通过设置解决了一个类似的问题
I solved a similar problem by setting
System.setProperty("http.keepAlive", "false");
在我进行 HTTP 调用之前.Android 在其连接池中保持关闭的连接并尝试重用它们似乎存在问题.
before I did my HTTP calls. There seems to be a problem with Android keep closed connections in its connection pool and trying to reuse them.
这篇关于Android SSL 错误:证书不受信任...有时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!