Java URL Value Check isUrlAvailable(URL url, Proxy proxy)

Here you can find the source of isUrlAvailable(URL url, Proxy proxy)

Description

Checks whether the specified URL points to a valid resource.

License

Open Source License

Parameter

Parameter Description
url The URL to check.
proxy An optional proxy definition. Can be null.

Exception

Parameter Description
IOException an exception
IllegalArgumentException an exception
UnsupportedOperationException an exception
ProtocolException an exception

Return

true if the URL is available, false otherwise.

Declaration

public static boolean isUrlAvailable(URL url, Proxy proxy)
        throws IOException, IllegalArgumentException, UnsupportedOperationException, ProtocolException 

Method Source Code

//package com.java2s;
// See LICENSE.txt for license information

import java.io.IOException;
import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.ProtocolException;
import java.net.Proxy;

import java.net.URL;
import java.net.URLConnection;

public class Main {
    /**//from w  ww. j  a va2  s .  co m
     * Checks whether the specified URL points to a valid resource.
     * @param url The URL to check.
     * @param proxy An optional proxy definition. Can be null.
     * @return true if the URL is available, false otherwise.
     * @throws IOException
     * @throws IllegalArgumentException
     * @throws UnsupportedOperationException
     * @throws ProtocolException
     */
    public static boolean isUrlAvailable(URL url, Proxy proxy)
            throws IOException, IllegalArgumentException, UnsupportedOperationException, ProtocolException {
        if (url != null) {
            if (url.getProtocol().equalsIgnoreCase("http") || url.getProtocol().equalsIgnoreCase("https")) {
                // We only need to check header for HTTP protocol
                HttpURLConnection.setFollowRedirects(true);
                HttpURLConnection conn = null;
                if (proxy != null) {
                    conn = (HttpURLConnection) url.openConnection(proxy);
                } else {
                    conn = (HttpURLConnection) url.openConnection();
                }
                if (conn != null) {
                    int timeout = conn.getConnectTimeout();
                    conn.setConnectTimeout(6000);
                    conn.setRequestMethod("HEAD");
                    int responseCode;
                    try {
                        responseCode = conn.getResponseCode();
                        conn.setConnectTimeout(timeout);
                        return (responseCode == HttpURLConnection.HTTP_OK);
                    } catch (IOException e) {
                        conn.setConnectTimeout(timeout);
                    }
                }
            } else {
                // more generic method
                InputStream is = null;
                URLConnection conn = null;
                if (proxy != null) {
                    conn = url.openConnection(proxy);
                } else {
                    conn = url.openConnection();
                }
                if (conn != null) {
                    is = url.openStream();
                    is.close();
                    return true;
                }
            }
        }
        return false;
    }
}

Related

  1. isURL(String str)
  2. isURL(String token)
  3. isUrl(String value)
  4. isURLAccessible(String URL)
  5. isURLAvailable(String url, int timeout)
  6. isUrlDownWithRetries(String url, long timeoutMs, long retryDelayMs)
  7. isUrlExists(String url)
  8. isURLExists(String URLName)
  9. isUrlInJar(URL url)