Using Method-1 to connect works, it trusts all certificates, even self signed certs, but the timeout does not work as well as Method-2
But when I use Method-2, not all certs are trusted and it fails for self signed certs. I understand that this is because in Method-2 I did not use my sslSocfactory which incorporates my trustAllCerts. I can't figure out how to incorporate my sslSocfactory into Method-2. I've tried a few way but the compile fails.
//=== my code ===========
SSLSocket socket = null;
SSLContext sc = null;
sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.g etSocketFactory());
SSLSocketFactory sslSocfactory = HttpsURLConnection.getDefaultSSLSocketFactory();
//--- Method-1 connection -----
socket = (SSLSocket)sslSocfactory.createSocket(host, port);
socket.setSoTimeout(timeout);
//--- Method-2 connection -----
socket = (SSLSocket)SSLSocketFactory.getDefault().createSoc ket();
socket.connect(new InetSocketAddress(host, port), timeout); //- This is the timeout that works for my situation
socket.setSoTimeout(timeout);