Example usage for java.net HttpURLConnection getResponseCode

List of usage examples for java.net HttpURLConnection getResponseCode

Introduction

In this page you can find the example usage for java.net HttpURLConnection getResponseCode.

Prototype

public int getResponseCode() throws IOException 

Source Link

Document

Gets the status code from an HTTP response message.

Usage

From source file:Main.java

public static String requestPage(String urlPath) throws Exception {
    URL url = new URL(urlPath);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    int responseCode = connection.getResponseCode();
    if (responseCode != 200) {
        throw new Exception("Error loading page: " + responseCode + connection.getResponseMessage());
    }//from  ww  w .jav a 2  s  . c o m

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String inputLine;
    String response = "";

    while ((inputLine = in.readLine()) != null) {
        response += inputLine;
    }

    in.close();
    return response;
}

From source file:Main.java

public static boolean isAuthorizationRequired(HttpURLConnection urlConnection) throws IOException {
    return isAuthorizationRequired(urlConnection.getResponseCode(),
            urlConnection.getHeaderField(WWW_AUTHENTICATE_HEADER));
}

From source file:Main.java

public static int getRequest(String link) throws IOException {
    URL url = new URL(link);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    System.out.println(connection.getResponseCode());
    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK || connection.getResponseCode() == 302) {
        InputStream is = connection.getInputStream();
        setParsedString(convertInputStreamToString(is));
    }//  www.  j  av  a  2 s .  c o m
    return connection.getResponseCode();
}

From source file:Main.java

public static byte[] getHtmlByteArray(final String url) {
    URL htmlUrl;//  www  . ja  v  a 2  s.c o m
    InputStream inStream = null;
    try {
        htmlUrl = new URL(url);
        URLConnection connection = htmlUrl.openConnection();
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        int responseCode = httpConnection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            inStream = httpConnection.getInputStream();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    byte[] data = inputStreamToByte(inStream);
    return data;
}

From source file:Main.java

public static String SimpleHttp(String urlStr) throws Exception {

    URL url = new URL(urlStr);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    int resCode = conn.getResponseCode();
    InputStream input = null;/*from w  w  w. j a  v  a  2s  .  c  o m*/
    String result = null;
    if (resCode == 200) {
        input = conn.getInputStream();
        result = toString(input);
        input.close();
        conn.disconnect();
    } else {
        throw new Exception("connect failed");
    }

    return result;
}

From source file:Main.java

public static byte[] getHtmlByteArray(final String url) {
    URL htmlUrl = null;//from  www . j ava  2s .com
    InputStream inStream = null;
    try {
        htmlUrl = new URL(url);
        URLConnection connection = htmlUrl.openConnection();
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        int responseCode = httpConnection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            inStream = httpConnection.getInputStream();
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    byte[] data = inputStreamToByte(inStream);

    return data;
}

From source file:Main.java

public static boolean isURLAccessable(String url) throws MalformedURLException, IOException {
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setDoInput(true);// w ww.  j a  v a 2s  .  c om
    conn.setDoOutput(true);

    return conn.getResponseCode() == 200;
}

From source file:Main.java

public static InputStream getRequest(String path) throws Exception {
    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setConnectTimeout(5000);//from  ww  w.ja  v  a2  s.  c  om
    if (conn.getResponseCode() == 200) {
        return conn.getInputStream();
    }
    return null;
}

From source file:Main.java

public static boolean delete(String murl) throws Exception {
    URL url = new URL(murl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("DELETE");
    conn.setConnectTimeout(5000);/*from w w  w.ja  va  2s.co m*/
    if (conn.getResponseCode() == 204) {
        return true;
    }
    return false;
}

From source file:com.microsoftopentechnologies.adinteractiveauth.Program.java

private static void httpRequest(URL url) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.getResponseCode();
}