protected SSLSocket createSSLSocket ( final String host , final int port , final int connectionTimeout , final boolean trustAll , final boolean verifyHttpsHost ) throws IOException { final SocketFactory socketFactory = getSocketFactory ( proxy , true , trustAll , connectionTimeout ) ; final Socket socket ; if ( connectionTimeout < 0 ) { socket = socketFactory . createSocket ( host , port ) ; } else { // creates unconnected socket // unfortunately, this does not work always // sslSocket = (SSLSocket) socketFactory.createSocket(); // sslSocket.connect(new InetSocketAddress(host, port), connectionTimeout); // // Note: SSLSocketFactory has several create() methods. // Those that take arguments all connect immediately // and have no options for specifying a connection timeout. // // So, we have to create a socket and connect it (with a // connection timeout), then have the SSLSocketFactory wrap // the already-connected socket. // socket = Sockets . connect ( host , port , connectionTimeout ) ; //sock.setSoTimeout(readTimeout); //socket.connect(new InetSocketAddress(host, port), connectionTimeout); // continue to wrap this plain socket with ssl socket... } // wrap plain socket in an SSL socket SSLSocket sslSocket ; if ( socket instanceof SSLSocket ) { sslSocket = ( SSLSocket ) socket ; } else { if ( socketFactory instanceof SSLSocketFactory ) { sslSocket = ( SSLSocket ) ( ( SSLSocketFactory ) socketFactory ) . createSocket ( socket , host , port , true ) ; } else { sslSocket = ( SSLSocket ) ( getDefaultSSLSocketFactory ( trustAll ) ) . createSocket ( socket , host , port , true ) ; } } // sslSocket is now ready if ( secureEnabledProtocols != null ) { final String [ ] values = StringUtil . splitc ( secureEnabledProtocols , ' ' ) ; StringUtil . trimAll ( values ) ; sslSocket . setEnabledProtocols ( values ) ; } // set SSL parameters to allow host name verifier if ( verifyHttpsHost ) { final SSLParameters sslParams = new SSLParameters ( ) ; sslParams . setEndpointIdentificationAlgorithm ( "HTTPS" ) ; sslSocket . setSSLParameters ( sslParams ) ; } return sslSocket ; }
Creates a SSL socket . Enables default secure enabled protocols if specified .