Java HTTPclient中没有SSLContext的Handshake_failure

时间:2019-06-16 23:53:38

标签: java ssl jsse

我感到自己遇到了一个奇怪的问题,我编写了一个没有SSLContext的核心java http客户端,当我运行该程序以调用REST服务时,它因以下异常而开始失败。但是,当我将SSLContext添加到客户端时,它通过并且看到响应。

Initial Code without SSLContext :
String url = "https://URL";
    URL obj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();     
    con.setRequestMethod("POST");           
    con.setRequestProperty("CONTENT-TYPE", "application/json"); 
    String request = "{ json Request }";    
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.write(request.getBytes());
    wr.flush();
    wr.close();
    int responseCode = con.getResponseCode();
    System.out.println("Response Code : " + responseCode);

例外:         javax.net.ssl.SSLHandshakeException:收到致命警报:handshake_failure

During SSL debugging it is found that client server sends TSLv1.2 during ClientHello, however i dont see a ServerHello in return.
But when the below SSL code error disappeared

SSLContext sc = SSLContext.getInstance("TLSv1.2"); 
        sc.init(null, null, new java.security.SecureRandom());
        con.setSSLSocketFactory(sc.getSocketFactory());

帮助我了解如果未设置SSLContext以及添加SSLContext时的行为会发生什么情况。

0 个答案:

没有答案