Java URL Exist existsHttp(String URLName)

Here you can find the source of existsHttp(String URLName)

Description

exists Http

License

Open Source License

Declaration

static boolean existsHttp(String URLName) 

Method Source Code

//package com.java2s;
/*/*  w  w w.ja  v a 2 s .co  m*/
*   Copyright (C) 2011 Life Technologies Inc.
*
*   This program is free software: you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*   the Free Software Foundation, either version 2 of the License, or
*   (at your option) any later version.
*
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*
*   You should have received a copy of the GNU General Public License
*   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.net.HttpURLConnection;

import java.net.URL;

public class Main {
    static boolean existsHttp(String URLName) {
        try {
            HttpURLConnection.setFollowRedirects(false);

            // note : you may also need
            //        HttpURLConnection.setInstanceFollowRedirects(false)
            HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection();
            con.setRequestMethod("HEAD");
            con.setConnectTimeout(10000);
            int code = con.getResponseCode();

            boolean ex = (code == HttpURLConnection.HTTP_OK);
            p("Exists url " + URLName + ", code: " + code + "= " + ex);
            return ex;
        } catch (Exception e) {
            p("URL does not exist: " + e.getMessage());
            return false;
        }
    }

    private static void p(String string) {
        System.out.println("FileUtils:" + string);
    }
}

Related

  1. exists(String fileorUrl)
  2. exists(String url)
  3. exists(URL url)
  4. exists(URL url)
  5. exists(URL url)
  6. existsURL(String url)
  7. existsURL_bak(String url)