我正在测试javax.net.ssl.HttpsURLConnection。因为程序正在发出 HTTPS 请求,所以我认为我必须执行 setSSLSocketFactory 才能使请求正常工作。
但是程序在没有 setSSLSocketFactory 设置的情况下运行良好:
这是我的代码:
public class HttpsTest {
public static void main(String[] args) {
try {
URL requestUrl = new URL("https://www.google.com/?gws_rd=ssl");
HttpsURLConnection conn = (HttpsURLConnection) requestUrl.openConnection();
// conn.setSSLSocketFactory(sslSocketFactory);
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
System.out.println("getResponseCode: " + conn.getResponseCode());
br.close();
conn.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
// RESULT:
// Some HTML return by https://www.google.com/?gws_rd=ssl
// getResponseCode: 200 OK
}
}
我对 Https 有什么误解?为什么程序在没有 sslSocketFactory 设置的情况下运行? (HttpsURLConnection需要sslSocketFactory来验证服务器发送的SSL/TLS公共(public)证书)
谢谢!
最佳答案
I thought I had to do
setSSLSocketFactory()
in order to make request work
你不知道。它有一个默认值。
关于Java HttpsURLConnection 在没有 setSSLSocketFactory 设置的情况下成功执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35854166/