Menu

#24 Reuse SSLSocketFactory

open
nobody
None
5
2012-12-15
2012-01-20
No

WCClient uses a new instance of SSLSocketFactory every time it connects to VC. As a result, sockets cannot be reused. There is also a small performance issue that trustAllHttpsCertificates() is executed upon each connection when it actually needs to be executed only once per the lifetime of the application.

Suggested code changes:

private static volatile boolean initialized = false; // new code

private static void trustAllHttpsCertificates()
throws NoSuchAlgorithmException, KeyManagementException
{
if (initialized) return; // new code
initialized = true; // new code
TrustManager[] trustAllCerts = new TrustManager[1];
trustAllCerts[0] = new TrustAllManager();
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}

Discussion