Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SSL error: certificate not trusted...sometimes

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/

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.

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.

like image 803
d370urn3ur Avatar asked Oct 11 '22 10:10

d370urn3ur


1 Answers

I solved a similar problem by setting

System.setProperty("http.keepAlive", "false");

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.

like image 87
Ben Williams Avatar answered Oct 20 '22 10:10

Ben Williams