Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read error response body in Java

In Java, this code throws an exception when the HTTP result is 404 range:

URL url = new URL("http://stackoverflow.com/asdf404notfound"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.getInputStream(); // throws! 

In my case, I happen to know that the content is 404, but I'd still like to read the body of the response anyway.

(In my actual case the response code is 403, but the body of the response explains the reason for rejection, and I'd like to display that to the user.)

How can I access the response body?

like image 900
Dan Fabulich Avatar asked Mar 05 '09 01:03

Dan Fabulich


People also ask

How do you read a response body in Java?

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 message from error body in retrofit?

you can simply use "response. message(). toString()" which will give the same error string in a more readable format.

How do I get response message from HttpURLConnection?

The getResponseMessage is a method of Java HttpURLConnection class. This method is used to get response code from HTTP response message. For example, if the response code from a server is HTTP/1.0 200 OK or HTTP/1.0 404 Not Found, it extracts the string OK and Not Found else returns null if HTTP is not valid.

What is HTTP Response in Java?

An HTTP response. An HttpResponse is not created directly, but rather returned as a result of sending an HttpRequest . An HttpResponse is made available when the response status code and headers have been received, and typically after the response body has also been completely received.


2 Answers

Here is the bug report (close, will not fix, not a bug).

Their advice there is to code like this:

HttpURLConnection httpConn = (HttpURLConnection)_urlConnection; InputStream _is; if (httpConn.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {     _is = httpConn.getInputStream(); } else {      /* error from server */     _is = httpConn.getErrorStream(); } 
like image 120
TofuBeer Avatar answered Oct 21 '22 22:10

TofuBeer


It's the same problem I was having: HttpUrlConnection returns FileNotFoundException if you try to read the getInputStream() from the connection.
You should instead use getErrorStream() when the status code is higher than 400.

More than this, please be careful since it's not only 200 to be the success status code, even 201, 204, etc. are often used as success statuses.

Here is an example of how I went to manage it

... connection code code code ...  // Get the response code  int statusCode = connection.getResponseCode();  InputStream is = null;  if (statusCode >= 200 && statusCode < 400) {    // Create an InputStream in order to extract the response object    is = connection.getInputStream(); } else {    is = connection.getErrorStream(); }  ... callback/response to your handler.... 

In this way, you'll be able to get the needed response in both success and error cases.

Hope this helps!

like image 23
gpiazzese Avatar answered Oct 21 '22 23:10

gpiazzese