Java URL Value Check isUrlValid(final URL url)

Here you can find the source of isUrlValid(final URL url)

Description

Quickly checks to see whether URL is reachable.

License

Open Source License

Declaration

public static boolean isUrlValid(final URL url) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.net.HttpURLConnection;

import java.net.URL;

public class Main {
    /**/*from  w w w.ja va 2 s  .c  om*/
     * Quickly checks to see whether URL is reachable.
     */
    public static boolean isUrlValid(final URL url) {
        final int timeout = 2000; // time, in milliseconds, used for open a  link  referenced by this URLConnection. 

        if (url == null)
            return false;

        try {
            final HttpURLConnection huc = (HttpURLConnection) url.openConnection();
            huc.setRequestMethod("HEAD");
            huc.setConnectTimeout(timeout); // Using constant named
            final int responseCode = huc.getResponseCode();
            return responseCode == HttpURLConnection.HTTP_OK;

        } catch (IOException ex) {
            return false;
        }
    }
}

Related

  1. isUrlDownWithRetries(String url, long timeoutMs, long retryDelayMs)
  2. isUrlExists(String url)
  3. isURLExists(String URLName)
  4. isUrlInJar(URL url)
  5. isUrlResponding(String url)
  6. isUrlValid(URL url)
  7. isValid(String candidateUrl)
  8. isValid(String url)
  9. isValidChannelUrl(String url)