How to get HTTP response code for a URL in Java?

Original Post: http://stackoverflow.com/questions/6467848/how-to-get-http-response-code-for-a-url-in-java

URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();

int code = connection.getResponseCode();

This is by no means a robust example; you’ll need to handle IOExceptions and whatnot. But it should get you started.

http://docs.oracle.com/javase/6/docs/api/java/net/HttpURLConnection.html#getResponseCode%28%29

getRequestMethod

public String getRequestMethod()
Get the request method.
Returns:
the HTTP request method
See Also:
setRequestMethod(java.lang.String)

getResponseCode

public int getResponseCode()
                    throws IOException
Gets the status code from an HTTP response message. For example, in the case of the following status lines:

 HTTP/1.0 200 OK
 HTTP/1.0 401 Unauthorized

It will return 200 and 401 respectively. Returns -1 if no code can be discerned from the response (i.e., the response is not valid HTTP).

Returns:
the HTTP Status-Code, or -1
Throws:
IOException – if an error occurred connecting to the server.

getResponseMessage

public String getResponseMessage()
                          throws IOException
Gets the HTTP response message, if any, returned along with the response code from a server. From responses like:

 HTTP/1.0 200 OK
 HTTP/1.0 404 Not Found

Extracts the Strings “OK” and “Not Found” respectively. Returns null if none could be discerned from the responses (the result was not valid HTTP).

Returns:
the HTTP response message, or null
Throws:
IOException – if an error occurred connecting to the server.
URL url = new URL("http://www.google.com/humans.txt");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
int statusCode = http.getResponseCode();
class ResponseCodeCheck 
{

    public static void main (String args[]) throws Exception
    {

        URL url = new URL("http://google.com");
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();

        int code = connection.getResponseCode();
        System.out.println("Response code of the object is "+code);
        if (code==200)
        {
            System.out.println("OK");
        }
    }
}
import java.io.IOException;
import java.net.URL;
import java.net.HttpURLConnection;

public class API{
    public static void main(String args[]) throws IOException
    {
        URL url = new URL("http://www.google.com");
        HttpURLConnection http = (HttpURLConnection)url.openConnection();
        int statusCode = http.getResponseCode();
        System.out.println(statusCode);
    }
}
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");

. . . . . . .

System.out.println("Value" + connection.getResponseCode());
             System.out.println(connection.getResponseMessage());
             System.out.println("content"+connection.getContent());

This is what worked for me:

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

public class UrlHelpers {

    public static int getHTTPResponseStatusCode(String u) throws IOException {

        URL url = new URL(u);
        HttpURLConnection http = (HttpURLConnection)url.openConnection();
        return http.getResponseCode();
    }
}

Hope this helps someone 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s