I am testing javax.net.ssl.HttpsURLConnection. Because the program is making HTTPS request so I thought I have to do setSSLSocketFactory in order to make request work.
BUT the program run fine without setSSLSocketFactory setting:
Here is my code:
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
}
}
What do I misunderstand about Https? Why the program worked without sslSocketFactory setting? ( The HttpsURLConnection need sslSocketFactory to validate SSL/TLS public certificate sent by the Server )
Thank you!