Skip to content

Instantly share code, notes, and snippets.

@torsaaan
Last active January 31, 2018 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torsaaan/21a2de7caa8977c91077377adefe6fa3 to your computer and use it in GitHub Desktop.
Save torsaaan/21a2de7caa8977c91077377adefe6fa3 to your computer and use it in GitHub Desktop.
private boolean isLinkDead(String link)
{
HttpURLConnection connection = null;
int code;
try
{
URL url=new URL(link);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setInstanceFollowRedirects(true);
connection.setReadTimeout(10000);
code = connection.getResponseCode();
connection.disconnect();
}
catch (UnknownHostException e)
{
return true; //link is considered dead if host is unknown
}
catch (SocketTimeoutException e)
{
return false; //link is NOT considered dead if timeout occurred
}
catch (Exception e)
{
return false; //link is NOT considered dead in case of any other exception
}
//link is considered dead if 404 returned
return (code==404 ? true : false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment