Reputation: 1
SSLSocket socket = null;
SSLContext sc = null;
sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
SSLSocketFactory sslSocfactory = HttpsURLConnection.getDefaultSSLSocketFactory();
//--- Method-1 connection -----
socket = (SSLSocket)sslSocfactory.createSocket(host, port);
socket.setSoTimeout(timeout);
//--- Method-2 connection -----
socket = (SSLSocket)SSLSocketFactory.getDefault().createSocket();
socket.connect(new InetSocketAddress(host, port), timeout); //- This timeout works for my situation
socket.setSoTimeout(timeout);
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 the trustAllCerts. I can't figure out how to incorporate my sslSocfactory into Method-2. I've tried a few ways, but the compile fails.
Upvotes: 0
Views: 823