Saturday, October 7, 2017

Java HttpsURLConnection and TLS 1.2 | Enable TLS 1.1 and 1.2 for Clients on Java 7 | Enabling TLSv1.2 with HttpsUrlConnection

You will have to create an SSLContext to set the Protocoll:

then you just have to set the SSLContext to the HttpsURLConnection:

httpsCon.setSSLSocketFactory(sc.getSocketFactory());


package com.pkm;

import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;

public class TLSRequest {
    public static void main(String[] args) throws Exception {
        HttpsURLConnection connection = (HttpsURLConnection) new URL("https://pritom.com").openConnection();
        // TLSv1 | TLSv1.1 | TLSv1.2
        SSLContext sc = SSLContext.getInstance("TLSv1");
        sc.init(null, null, new java.security.SecureRandom()); 
        connection.setSSLSocketFactory(sc.getSocketFactory());
    }
}

No comments:

Post a Comment