Java URL Value Check isURLAccessible(String URL)

Here you can find the source of isURLAccessible(String URL)

Description

Finds out whether a URL returns HTTP OK or not.

License

Open Source License

Parameter

Parameter Description
URL URL to find out whether is accessible

Return

true if URL is accesible with HTTP OK exit code (200), false otherwise

Declaration

public static boolean isURLAccessible(String URL) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007-2017 Red Hat, Inc.
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is made available under the terms of the
 * Eclipse Public License v 1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributor://from ww w.j  a  v  a  2s.c o  m
 *     Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    /**
     * Finds out whether a URL returns HTTP OK or not.
     * 
     * @param URL URL to find out whether is accessible 
     * @return true if URL is accesible with HTTP OK exit code (200), false otherwise
     */
    public static boolean isURLAccessible(String URL) {
        try {
            HttpURLConnection.setFollowRedirects(false);
            HttpURLConnection connection = (HttpURLConnection) new URL(URL)
                    .openConnection();
            connection.setRequestMethod("HEAD");
            return (connection.getResponseCode() == HttpURLConnection.HTTP_OK);
        } catch (Exception e) {
            return false;
        }
    }
}

Related

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