0

I have a dashboard which is accessed via the VPN ,and the datapoints could be seen. However, we want to achieve that by making a REST call to the same dashboard link and extract the data. However, I encounter the following errors.

javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

I looked it up on the previous questions asked on the forum, and updated my certificate from the URL. I also have a authentication mechanism wherein I provide my credentials to access the data, the same has been coded on JAVA as well.

I am trying to reach the dasboard link via the VPN and my code is as follows:

private static HttpsURLConnection connection; 

    public static void main(String[] args) {
        BufferedReader reader;
        String line; //every line
        StringBuffer responseContent = new StringBuffer();
        
        try {
            URL url = new URL("https://XXXXXX/XXX/XX");
            connection = (HttpsURLConnection) url.openConnection();
            connection.setAuthenticator(new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("username", "pass".toCharArray());
                }
            });
            
            
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            
            
            int status = connection.getResponseCode();
            System.out.println(status);
            
            if(status>299) {
                reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
                while((line = reader.readLine())!=null) {
                    responseContent.append(line);
                }
            reader.close();
            }else {
                reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                while((line = reader.readLine())!=null) {
                    responseContent.append(line);
                }
                reader.close();
            }
            
        System.out.println(responseContent.toString());
            
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            connection.disconnect();
        }
    
        
    }

1 Answers1

0

The message is related to che PKIX validation of the server's certificate.

You should add it to the cacerts keystore of the java vm you use to run the code.

See this answer for more details.

gdegani
  • 846
  • 6
  • 17