Facing PKIX path building failed exception in selenium broken link script
Asked Answered
B

1

0

I am using below lines of code to verify broken links

   huc = (HttpURLConnection) (new URL(url).openConnection());
   huc.setRequestMethod("HEAD");
   huc.setReadTimeout(5000);
   huc.connect();
   respCode = huc.getResponseCode();

when script enters huc.connect() line , it throws below exception.

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

How can I solve it? I need to run this script locally and also in github actions pipeline.

Bludgeon answered 4/7, 2022 at 16:26 Comment(1)
How did you declare huc?Lorgnon
F
2

You get this error when your Website certificate is not added to Trusted store. Please follow below step to add certificate to trusted store.

  1. Open the website and Download the certificate by clicking on the lock icon->Connection is secure->Certification-> click on the root->Certification path->Copy to file-> and store in your local system with an alias name.

cert1 cert2 cert3

  1. Now you need to add certificate to $JAVA_HOME\lib\security\cacerts using below command
keytool -import -noprompt -trustcacerts -alias <AliasName> -file   <certificate> -keystore <KeystoreFile> -storepass <Password>

eg:

keytool -import -noprompt -trustcacerts -alias myaddedcertaliasname
 -file /localsystempath/to/my/cert/myCert.cer -keystore /path/to/my/jdk/jre/lib/security/cacerts/keystore.jks -storepass
 changeit

Please note the default password for key store is changeit.

Once you import the certificate to your key store you should no more get this error

Foreandaft answered 5/7, 2022 at 0:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.