3

I'm trying to verify if a link is throwing response code other than 200. Link, that I'm trying to verify is https:// and after process has run, it throws this error --

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

Code that I'm using --

URL h1 = new URL(crazyLink)
HttpURLConnection huc1 = (HttpURLConnection) h1.openConnection();
huc1.setRequestMethod("GET");
huc1.connect();
if(huc1.getResponseCode() == 200){
    System.out.println("Passed");
}else{
    System.out.println("Failed");
}

I've tried changing FirefoxProfile to ignore ssl, but it didn't work --

firefoxProfile profile=new firefoxProfile();
Profile.setAssumeUntrustedCertificateIssuer(false);
driver=new firefoxDriver(profile);

Any ideas?

3
  • Does the url you are trying to hit require authentication? Does it prompt for a user/password if you hit it directly within your browser in an incognito window?
    – Sam Woods
    Sep 24, 2014 at 16:48
  • @SamWoods I tried in incognito window by directly pasting the link. It doesn't require any authentication. The link itself has a guid and goes directly in the landing page which does not have an authentication page. If I tried without the guid, it does go to the authentication page. Please let me know if you need any more info. Thank you so much for responding.
    – user7962
    Sep 24, 2014 at 17:21
  • Which FirefoxWebDriver version are you using?
    – dzieciou
    Sep 25, 2014 at 5:15

2 Answers 2

1

The exception is not related to HTTP Basic Authentication, i.e., is not about missing authentication credentials. This also has nothing to do with broken links. This bizzarre exception message is saying that in your truststore you do not have certificate of the Web site you want to connect to, so simply that you do not trust this site.

Now the solution depends on which of your code excerpts we're talking about. For the first you should use HttpsURLConnection instead of HttpURLConnection, because you're trying to connect to SSL-protected URL. Then you should:

For the second code which uses WebDriver it may depend on which library version are using. So which one are you using?

1

True to edit user3783352's answer but got some permission error. So here is the content of the post referred to in that answer-

package <Your Package>;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CheckLinks {
public static int invalidLink;
String currentLink;
String temp;
public static int responsecode;
public static boolean isValid = false;
public static void main(String[] args) throws IOException
{
// Launch The Browser
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Enter Url
driver.get(“<URLre>”);

//Check css & favicon
try
{
List<WebElement> no = driver.findElements(By.tagName(“link”));
int nooflinks = no.size();
System.out.println(“Total CSS & Favicon Icons: ” + nooflinks);
for (WebElement pagelink : no)
{
String linktext = pagelink.getText();
String link = pagelink.getAttribute(“href”);
URL u = new URL(link);
HttpURLConnection h = (HttpURLConnection) u.openConnection();
h.setRequestMethod(“GET”);
h.connect();
int responsecode = h.getResponseCode();
System.out.println(linktext+”\t” + link + “\t” + “Response Code: ” + responsecode);
//System.out.println(link);
}
}
catch (Exception e)
{

}

//Check Javascripts
try
{
List<WebElement> no = driver.findElements(By.tagName(“script”));
int nooflinks = no.size();
System.out.println(“Total Javascripts: ” + nooflinks);
for (WebElement pagelink : no)
{
String linktext = pagelink.getText();
String link = pagelink.getAttribute(“src”);
URL u = new URL(link);
HttpURLConnection h = (HttpURLConnection) u.openConnection();
h.setRequestMethod(“GET”);
h.connect();
int responsecode = h.getResponseCode();
System.out.println(linktext+”\t” + link + “\t” + “Response Code: ” + responsecode);
//System.out.println(link);
}
}
catch (Exception e)
{

}

//Check Images
try
{
List<WebElement> no = driver.findElements(By.tagName(“img”));
int nooflinks = no.size();
System.out.println(“Total Images: ” + nooflinks);
for (WebElement pagelink : no)
{
String linktext = pagelink.getAttribute(“alt”);
String link = pagelink.getAttribute(“src”);
URL u = new URL(link);
HttpURLConnection h = (HttpURLConnection) u.openConnection();
h.setRequestMethod(“GET”);
h.connect();
int responsecode = h.getResponseCode();
System.out.println(linktext+”\t” + link + “\t” + “Response Code: ” + responsecode);
//System.out.println(link);
}
}
catch (Exception e)
{

}

//Check hyperlinks.
try
{
List<WebElement> no = driver.findElements(By.tagName(“a”));
int nooflinks = no.size();
System.out.println(“Total hyperlinks: ” + nooflinks);
for (WebElement pagelink : no)
{
String linktext = pagelink.getText();
String link = pagelink.getAttribute(“href”);
URL u = new URL(link);
HttpURLConnection h = (HttpURLConnection) u.openConnection();
h.setRequestMethod(“GET”);
h.connect();
int responsecode = h.getResponseCode();
System.out.println(linktext+”\t” + link + “\t” + “Response Code: ” + responsecode);
//System.out.println(link);
}
}
catch (Exception e)
{

}
System.out.println(“— End of Test —“);

driver.close();
driver.quit();
}
}

Now I can easily check broken links on a web page with this piece of code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.