Java: How to check if a web page exists/available?
import java.net.HttpURLConnection;
import java.net.URL;
public class URLUtils {
public static void main(String[] args) {
System.out.println(URLUtils.checkIfURLExists("http://www.google.com/"));
}
public static boolean checkIfURLExists(String targetUrl) {
HttpURLConnection httpUrlConn;
try {
httpUrlConn = (HttpURLConnection) new URL(targetUrl)
.openConnection();
// A HEAD request is just like a GET request, except that it asks
// the server to return the response headers only, and not the
// actual resource (i.e. no message body).
// This is useful to check characteristics of a resource without
// actually downloading it,thus saving bandwidth. Use HEAD when
// you don't actually need a file's contents.
httpUrlConn.setRequestMethod("HEAD");
// Set timeouts in milliseconds
httpUrlConn.setConnectTimeout(30000);
httpUrlConn.setReadTimeout(30000);
// Print HTTP status code/message for your information.
System.out.println("Response Code: "
+ httpUrlConn.getResponseCode());
System.out.println("Response Message: "
+ httpUrlConn.getResponseMessage());
return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
return false;
}
}
}

2 Comments
Jump to comment form | comment rss [?] | trackback uri [?]