How to connect to TLS 1.2 enabled URL with Java [closed]

ぐ巨炮叔叔 提交于 2019-12-07 00:26:42

问题


How to connect to TLS 1.2 enabled URL using HTTP POST Method.


回答1:


Java 8

Java 8 will use TLS 1.2 by default

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

Java 7 needs to be set manually

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());


来源:https://stackoverflow.com/questions/44788781/how-to-connect-to-tls-1-2-enabled-url-with-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!