本文介绍了如何使用 Java 连接到启用 TLS 1.2 的 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 HTTP POST 方法连接到启用了 TLS 1.2 的 URL.
解决方案
Java 8
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.*;导入 java.net.URL;URL url = new URL("https://www.google.com");HttpsURLConnection 连接 = (HttpsURLConnection) url.openConnection();
Java 7
Java 7 需要手动设置
import java.security.*;导入 javax.net.ssl.*;导入 java.net.URL;URL url = new URL("https://www.google.com");SSLContext ssl = SSLContext.getInstance("TLSv1.2");ssl.init(null, null, new SecureRandom());HttpsURLConnection 连接 = (HttpsURLConnection) url.openConnection();connection.setSSLSocketFactory(ssl.getSocketFactory());
How to connect to TLS 1.2 enabled URL using HTTP POST Method.
解决方案
Java 8
https://blogs.oracle.com/java-platform-group/jdk-8-will-use-tls-12-as-default
So for Java 8 all you need to do is the following.
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());
这篇关于如何使用 Java 连接到启用 TLS 1.2 的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!