Java URL Value Check isHttpURLAvailable(final String url)

Here you can find the source of isHttpURLAvailable(final String url)

Description

Executes an HTTP GET Request to the given URL, using a one second connect timeout and read timeout.

License

Open Source License

Parameter

Parameter Description
url the HTTP URL.

Return

the HTTP return code. If an error occured while sending the request, for instance if a connection could not be made, returns 500

Declaration

public static boolean isHttpURLAvailable(final String url) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 GigaSpaces Technologies Ltd. All rights reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License./*from w ww  .  ja  v  a2  s  .  co m*/
 *******************************************************************************/

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

import java.net.MalformedURLException;
import java.net.ProtocolException;

import java.net.URL;

public class Main {
    private static final int DEFAULT_HTTP_READ_TIMEOUT = 1000;
    private static final int DEFAULT_HTTP_CONNECTION_TIMEOUT = 1000;

    /*********
     * Executes an HTTP GET Request to the given URL, using a one second connect timeout and read timeout.
     * 
     * @param url the HTTP URL.
     * @return the HTTP return code. If an error occured while sending the request, for instance if a connection could
     *         not be made, returns 500
     */
    public static boolean isHttpURLAvailable(final String url) {
        return getHttpReturnCode(url) == HttpURLConnection.HTTP_OK;
    }

    /*********
     * Executes an HTTP GET Request to the given URL, using a one second connect timeout and read timeout.
     * 
     * @param url the HTTP URL.
     * @return the HTTP return code. If an error occured while sending the request, for instance if a connection could
     *         not be made, returns 500
     */
    public static int getHttpReturnCode(final String url) {
        return getHttpReturnCode(url, DEFAULT_HTTP_CONNECTION_TIMEOUT, DEFAULT_HTTP_READ_TIMEOUT);
    }

    /*********
     * Executes an HTTP GET Request to the given URL.
     * 
     * @param url the HTTP URL.
     * @param connectTimeout the connection timeout.
     * @param readTimeout the read timeout.
     * @return the HTTP return code. If an error occured while sending the request, for instance if a connection could
     *         not be made, returns 500
     */
    public static int getHttpReturnCode(final String url, final int connectTimeout, final int readTimeout) {

        HttpURLConnection connection = null;

        try {
            try {
                connection = (HttpURLConnection) new URL(url).openConnection();
            } catch (final MalformedURLException e) {
                throw new IllegalArgumentException("Failed to parse url: " + url, e);
            }
            try {
                connection.setRequestMethod("GET");
            } catch (final ProtocolException e) {
                throw new IllegalArgumentException(e);
            }

            connection.setConnectTimeout(connectTimeout);
            connection.setReadTimeout(readTimeout);

            final int responseCode = connection.getResponseCode();
            return responseCode;
        } catch (final IOException ioe) {
            return HttpURLConnection.HTTP_INTERNAL_ERROR;
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
    }
}

Related

  1. isHttpUrl(final URI toCheck)
  2. isHttpUrl(final URL url)
  3. isHttpUrl(String in)
  4. isHttpUrl(URL url)
  5. isHttpUrl(URL url)
  6. isInternetReachable(String url)
  7. isJar(URL url)
  8. isJarFile(URL url)
  9. isJarFile(URLConnection connection)