I have to find out broken links in LinkedIn links file. I am writing code as below in Selenium Web driver but it still shows the valid links:
for (String url : linkedinURLs) {
try {
URL link = new URL(url);
HttpURLConnection httpConn = (HttpURLConnection) link.openConnection();
httpConn.setConnectTimeout(5000);
httpConn.setInstanceFollowRedirects(true);
httpConn.connect();
// Get the final URL after following redirects
String finalUrl = httpConn.getURL().toString();
if (httpConn.getResponseCode() >= 400) {
System.out.println("Invalid URL: " + finalUrl);
}
httpConn.disconnect(); // Close the connection
} catch (IOException e) {
// Handle any exceptions that occur during the HTTP request
System.err.println("Error while checking URL: " + url);
e.printStackTrace();
}
}