Java HttpsURLConnection and TLS 1.2

前端 未结 3 406
旧时难觅i
旧时难觅i 2020-12-05 00:35

I read in an article that HttpsURLConnection will transparently negotiate the SSL connection.

The official document says:

This c

3条回答
  •  悲哀的现实
    2020-12-05 00:49

    private static javax.net.ssl.SSLSocketFactory getFactorySimple() 
            throws Exception {
        SSLContext context = SSLContext.getInstance("TLSv1.2");`
    
        context.init(null, null, null);
    
        return context.getSocketFactory();
    
    }
    
    String loginurl ="some url";
    HttpsURLConnection connection = null;
    URL url = new URL(loginURL);
    
    connection = (HttpsURLConnection) url.openConnection();
    
    javax.net.ssl.SSLSocketFactory sslSocketFactory =getFactorySimple();
    
    connection.setSSLSocketFactory(sslSocketFactory);
    

    The above code can be used to enable tls 1.1 or tls 1.2 in java 1.7

提交回复
热议问题