Java URL Read getUrlStatus(String url)

Here you can find the source of getUrlStatus(String url)

Description

Get the http status of url

License

Open Source License

Parameter

Parameter Description
url will be checked

Return

the http status of url, if any exception throwed, it will return 0

Declaration

public static int getUrlStatus(String url) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**/*  ww w  .j a  v  a 2  s . co m*/
     * Get the http status of url
     * 
     * @param url will be checked
     * @return the http status of url, if any exception throwed, it will return
     *         0
     */
    public static int getUrlStatus(String url) {
        HttpURLConnection conn = null;

        try {
            URL u = new URL(url);
            conn = (HttpURLConnection) u.openConnection();
            conn.connect();
            return conn.getResponseCode();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
        } finally {
            conn.disconnect();
        }
        return 0;

    }
}

Related

  1. getURLContent_old(final String uri, final StringBuffer content)
  2. getUrlContentWithRetries(String url, long timeoutMs, long retryDelayMs)
  3. getUrlFollowingRedirects(String possibleRedirectionUrl)
  4. getUrlInfos(String urlAsString, int timeout)
  5. getUrlSource(String url)
  6. getUrlTxt(String url)
  7. readAsString(final URL url)
  8. readListFromURL(URL p_url)
  9. readPage(String url)