Java URL Value Check is404(URL url)

Here you can find the source of is404(URL url)

Description

is

License

Open Source License

Declaration

public static boolean is404(URL url) 

Method Source Code

//package com.java2s;
/* /*from w w w .  ja  va  2 s .  c om*/
This file is part of DeltaLauncher.
    
DeltaLauncher is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
DeltaLauncher 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 Lesser General Public License for more details.
    
You should have received a copy of the Lesser GNU General Public License
along with DeltaLauncher.  If not, see <http://www.gnu.org/licenses/>.
*/

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

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

public class Main {
    public static boolean is404(URL url) {

        URLConnection connection;
        InputStream urlIn = null;
        try {
            connection = url.openConnection();

            //If the connection is http, let's try if the response code is an error (>= 400)
            if (connection instanceof HttpURLConnection) {
                HttpURLConnection httpConnection = (HttpURLConnection) connection;
                return httpConnection.getResponseCode() >= 400;
            }

            //In this case, the connection is not http, so it could be file://, or an other unkown protocol.
            //we'll just try to open an input stream and read ; if we manage to do this without IOException, then false will be returned.
            //Otherwise, an IOException will be threw up and true will be returned.
            urlIn = connection.getInputStream();
            urlIn.read();

        } catch (IOException ex) {
            return true;
        } finally {
            try {
                if (urlIn != null) {
                    urlIn.close();
                }
            } catch (IOException e) {
            }
        }

        return false;
    }
}

Related

  1. checkURLStatus(String pURL)
  2. checkURLValidProtocol(URL url)
  3. invokePostUrl(String url, String encodedQueryString)
  4. invokeUrl(String url)
  5. is200(final String url)
  6. isAddressReachable(String url, int timeout)
  7. isAValidURL(String url)
  8. isCompressed(HttpURLConnection connection)
  9. isEMailAddress(URL url)