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!
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.
Use HttpResponse. getStatusLine() , which returns a StatusLine object containing the status code, protocol version and "reason". Show activity on this post.
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.
Use HttpResponse.getStatusLine()
, which returns a StatusLine
object containing the status code, protocol version and "reason".
I have used httpResponse.getStatusLine().getStatusCode()
and have found this to reliably return the integer http status code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With