Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get HTTP code from org.apache.http.HttpResponse

I'm using the org.apache.http.HttpResponse class in my Java application, and I need to be able to get the HTTP status code. If I used .toString() on it, I can see the HTTP status code in there. Is there any other function that I can just get the HTTP status code as either an int or String?

Thanks a bunch!

like image 741
Chiggins Avatar asked May 27 '11 14:05

Chiggins


People also ask

How do I get response body from Org Apache HttpResponse?

To get the response body as a string we can use the EntityUtils. toString() method. This method read the content of an HttpEntity object content and return it as a string. The content will be converted using the character set from the entity object.

How do I get response code from HTTP response?

Use HttpResponse. getStatusLine() , which returns a StatusLine object containing the status code, protocol version and "reason". Show activity on this post.

How do I find HttpEntity?

The HttpEntity can be obtained from the HttpResponse object. From the ContentType object we can get the mime-type by calling the getMimeType() method. This method will return a string value. To get the charset we can call the getCharset() method which will return a java.


2 Answers

Use HttpResponse.getStatusLine(), which returns a StatusLine object containing the status code, protocol version and "reason".

like image 177
matt b Avatar answered Oct 12 '22 12:10

matt b


I have used httpResponse.getStatusLine().getStatusCode() and have found this to reliably return the integer http status code.

like image 27
user1735872 Avatar answered Oct 12 '22 10:10

user1735872