如何使用HTTP POST方法连接到启用了TLS 1.2的URL。
答案 0 :(得分:12)
Java 8默认使用TLS 1.2
https://blogs.oracle.com/java-platform-group/jdk-8-will-use-tls-12-as-default
因此,对于Java 8,您需要做的就是以下内容。
import javax.net.ssl.*;
import java.net.URL;
URL url = new URL("https://www.google.com");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
需要手动设置Java 7
import java.security.*;
import javax.net.ssl.*;
import java.net.URL;
URL url = new URL("https://www.google.com");
SSLContext ssl = SSLContext.getInstance("TLSv1.2");
ssl.init(null, null, new SecureRandom());
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setSSLSocketFactory(ssl.getSocketFactory());