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 void main(String[] argv) throws Exception {
    HttpURLConnection.setFollowRedirects(false);
    HttpURLConnection con = (HttpURLConnection) new URL("http://www.google.coom").openConnection();
    con.setRequestMethod("HEAD");
    System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_PAYMENT_REQUIRED);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    HttpURLConnection.setFollowRedirects(false);
    HttpURLConnection con = (HttpURLConnection) new URL("http://www.google.coom").openConnection();
    con.setRequestMethod("HEAD");
    System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_OK);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    HttpURLConnection.setFollowRedirects(false);
    HttpURLConnection con = (HttpURLConnection) new URL("http://www.google.coom").openConnection();
    con.setRequestMethod("HEAD");
    System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_PARTIAL);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    HttpURLConnection.setFollowRedirects(false);
    HttpURLConnection con = (HttpURLConnection) new URL("http://www.google.coom").openConnection();
    con.setRequestMethod("HEAD");
    System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    HttpURLConnection.setFollowRedirects(false);
    HttpURLConnection con = (HttpURLConnection) new URL("http://www.google.coom").openConnection();
    con.setRequestMethod("HEAD");
    System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_NOT_AUTHORITATIVE);
}

From source file:com.trendmicro.hdfs.webdav.tool.Get.java

public static void main(String[] args) throws Exception {
    // Process command line 

    Options options = new Options();
    options.addOption("d", "debug", false, "Enable debug logging");

    CommandLine cmd = null;//from  w ww  .  j a va2s.  c o m
    try {
        cmd = new PosixParser().parse(options, args);
    } catch (ParseException e) {
        printUsageAndExit(options, -1);
    }
    boolean debug = cmd.hasOption('d');
    args = cmd.getArgs();
    if (args.length < 1) {
        printUsageAndExit(options, -1);
    }

    // Do the fetch

    AuthenticatedURL.Token token = new AuthenticatedURL.Token();
    AuthenticatedURL url = new AuthenticatedURL();
    HttpURLConnection conn = url.openConnection(new URL(args[0]), token);
    if (debug) {
        System.out.println("Token value: " + token);
        System.out.println("Status code: " + conn.getResponseCode() + " " + conn.getResponseMessage());
    }
    if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line = reader.readLine();
        while (line != null) {
            System.out.println(line);
            line = reader.readLine();
        }
        reader.close();
    }
    System.out.println();
}

From source file:org.javaee7.ejb.stateless.remote.AccountSessionBeanWithInterface.java

public static void main(String[] args) {
    try {/*from  w  w w . j a  va 2s.  co  m*/
        ObjectMapper mapper = new ObjectMapper();
        URL url = new URL("http://localhost:8180/account-1.0-SNAPSHOT/statistics");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");

        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

        String output;
        String result = null;
        while ((output = br.readLine()) != null) {
            result = result + output;
        }
        conn.disconnect();

    } catch (MalformedURLException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Properties systemSettings = System.getProperties();
    systemSettings.put("proxySet", "true");
    systemSettings.put("http.proxyHost", "proxy.mycompany.local");
    systemSettings.put("http.proxyPort", "80");

    URL u = new URL("http://www.google.com");
    HttpURLConnection con = (HttpURLConnection) u.openConnection();
    BASE64Encoder encoder = new BASE64Encoder();
    String encodedUserPwd = encoder.encode("domain\\username:password".getBytes());
    con.setRequestProperty("Proxy-Authorization", "Basic " + encodedUserPwd);
    con.setRequestMethod("HEAD");
    System.out.println(con.getResponseCode() + " : " + con.getResponseMessage());
    System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_OK);
}

From source file:Main.java

public static void main(String s[]) throws Exception {
    try {//www  . j av a 2s . com
        Properties systemSettings = System.getProperties();
        systemSettings.put("proxySet", "true");
        systemSettings.put("http.proxyHost", "proxy.mycompany.local");
        systemSettings.put("http.proxyPort", "80");

        URL u = new URL("http://www.java.com");
        HttpURLConnection con = (HttpURLConnection) u.openConnection();
        sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
        String encodedUserPwd = encoder.encode("domain\\username:password".getBytes());
        con.setRequestProperty("Proxy-Authorization", "Basic " + encodedUserPwd);
        con.setRequestMethod("HEAD");
        System.out.println(con.getResponseCode() + " : " + con.getResponseMessage());
        System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_OK);
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(false);
    }
}

From source file:edu.mayo.informatics.lexgrid.convert.directConversions.UmlsCommon.LoadRRFToDB.java

public static void main(String[] args) throws Exception {
    URI testURI = new URI("http://www.cnn.com/");
    // URI testURI = new URI("file:///W:/temp");
    // System.out.println(new File(testURI).exists());

    HttpURLConnection connecition = (HttpURLConnection) testURI.toURL().openConnection();
    System.out.println(connecition.getResponseCode());

    System.out.println(testURI.resolve("TEST").toString());
}