Java URL Value Check isURLAvailable(String url, int timeout)

Here you can find the source of isURLAvailable(String url, int timeout)

Description

is URL Available

License

Apache License

Declaration

public static boolean isURLAvailable(String url, int timeout) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.net.*;

public class Main {

    public static boolean isURLAvailable(String url, int timeout) {
        try {/*from ww w.  j  a va 2s .  c  o  m*/
            return isURLAvailable(new URL(url), timeout);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean isURLAvailable(URL url, int timeout) {
        try {
            HttpURLConnection.setFollowRedirects(false);
            // note : you may also need
            //        HttpURLConnection.setInstanceFollowRedirects(false)
            HttpURLConnection con = (HttpURLConnection) url
                    .openConnection();
            con.setRequestMethod("HEAD");
            con.setConnectTimeout(timeout);
            return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
        } catch (Exception e) {
            //  e.printStackTrace();
            return false;
        }
    }
}

Related

  1. isUrl(String s)
  2. isURL(String str)
  3. isURL(String token)
  4. isUrl(String value)
  5. isURLAccessible(String URL)
  6. isUrlAvailable(URL url, Proxy proxy)
  7. isUrlDownWithRetries(String url, long timeoutMs, long retryDelayMs)
  8. isUrlExists(String url)
  9. isURLExists(String URLName)