How to solve javax.net.ssl.SSLHandshakeException Error?

前端 未结 6 523
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 16:57

I connected with VPN to setup the inventory API to get product list and it works fine. Once I get the result from the web-service and i bind to UI. And also I integrated Pay

6条回答
  •  情书的邮戳
    2020-11-22 17:40

    Now I solved this issue in this way,

    import javax.net.ssl.HttpsURLConnection;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.TrustManager;
    import javax.net.ssl.X509TrustManager;
    import java.io.OutputStream; 
    
    // Create a trust manager that does not validate certificate chains like the default 
    
    TrustManager[] trustAllCerts = new TrustManager[]{
            new X509TrustManager() {
    
                public java.security.cert.X509Certificate[] getAcceptedIssuers()
                {
                    return null;
                }
                public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
                {
                    //No need to implement.
                }
                public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
                {
                    //No need to implement.
                }
            }
    };
    
    // Install the all-trusting trust manager
    try 
    {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } 
    catch (Exception e) 
    {
        System.out.println(e);
    }
    

    Of course this solution should only be used in scenarios, where it is not possible to install the required certifcates using keytool e.g. local testing with temporary certifcates.

提交回复
热议问题