Last active
January 31, 2018 08:27
-
-
Save torsaaan/21a2de7caa8977c91077377adefe6fa3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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