Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an URL and i want only to check the response code of the page and not the complete page source as fetching the the complete page source is quite slow. what is right way to go ?

does getResponseCode() in HttpUrlConnection feches the complete page source or only the header ?

share|improve this question
int respCode = ((HttpURLConnection)conn).getResponseCode(); will return response code – shreyansh jogi yesterday
I would say, no, it doesn't download the content. – MadProgrammer yesterday

3 Answers

up vote 2 down vote accepted

Straight from the docs, HttpUrlConnection#getResponseCode()

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).

share|improve this answer
Here method may be PostMethod or GetMethod 

you can get status code from an HTTP response message by getStatusCode()

example:- int statuscode=method.getStatusCode();

share|improve this answer

Depends what you're motivation is in making the request. If the request is normally just a GET request for a resource and doesn't have any side effects.

You can perform a HTTP HEAD request instead, which if implemented correctly should get you the same status codes but not the body. (i.e. setRequestMethod(HEAD))

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.