Java: How to check if a web page exists/available?


public class URLUtils {

	public static void main(String[] args) {

	public static boolean checkIfURLExists(String targetUrl) {
		HttpURLConnection httpUrlConn;
		try {
			httpUrlConn = (HttpURLConnection) new URL(targetUrl)

			// 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.

			// Set timeouts in milliseconds

			// 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;



  1. Paolo · · Reply

    URLUtils cannot be resolved

    1. Paolo · · Reply

      ehm.. sorry.. nothing :)

  2. It just gives me an error and rtn’s false for working webpages ?

    1. What error you see and what’s the webpage you’re trying to check for availability?

  3. Doesn’t work with HTTPS… what are setting I have to

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 45 other followers

%d bloggers like this: