I already read the link checker question posted in SO. If there are more questions of this kind and I missed them, my apologies.
We need to find broken links in a website in a very easy way and scriptable way, so we can document the links that are broken. I found a blog post with some code written in java that will do exactly what I need, and my very basic knowledge let me compile it, but I get errors every time. I thought that maybe someone here might be able to direct me to why the code does not compile.
Here is the code:
import java.net.HttpURLConnection;
import java.net.URL;
class links
{
private static boolean isLive(String link) {
HttpURLConnection urlConnection = null;
try {
URL url = new URL(link);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("HEAD");
urlConnection.connect();
String redirectLink = urlConnection.getHeaderField("Location");
if (redirectLink != null && !url.equals(redirectLink)) {
return isLive(redirectLink);
} else {
return urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK;
}
} catch (Exception e) {
return false;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
public static void main(String[] args) {
System.out.println(isLive("http://www.fakelink.net"));
}
}
Thanks to all that reply. I putting the code that compiles here, for future reference.