Thursday, 22 August 2013

get http response code from a url

get http response code from a url

I want to write a Java program which will hit a URL and will print the
status code(i.e., 200, 404, etc.). I am doing this using the
HttpUrlConnection api, but it only shows the exception, and does not print
the status code.
URL url = new URL("https://abc.com/test.html");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
System.out.println("code: "+code);

No comments:

Post a Comment