Retrieving server side HTTP error messages in Java

2.4k views Asked by At

I'm building a backend server side java application that uses a post method to send data to an API, based on an example I've been given in Python and C# (console applications). This API has specific responses associated with various response codes. I tried using the code below, but the response message just seems to be the generic system message associated with the response code, not the message on the API I'm accessing.

 System.out.println("Response Code : " + connection.getResponseCode());
 System.out.println("Response Message : " + connection.getResponseMessage());

I saw in the C# example that they were using WebException error handling, but I can't find the Java equivalent, if there even is one.

Edit: This is the error I'm getting with a 403, in the python example, it returned a custom response

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://[redacted]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1926)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1921)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1920)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1490)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at javaapplication1.JavaApplication1.main(JavaApplication1.java:59)
Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: https://api.rentrak.com/tv/v3/national_airing_views
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
at javaapplication1.JavaApplication1.main(JavaApplication1.java:56)

This is the code I'm using to build the response

    StringBuilder response;
    try (BufferedReader in = new BufferedReader(
            new InputStreamReader(connection.getInputStream()))) {
        String inputLine;
        response = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
    }
1

There are 1 answers

8
Vadim On BEST ANSWER

If you use just java.net.HttpURLConnection

for success response there is connection.getInputStream() to get response body.

But for error response there is connection.getErrorStream() where error response body will be.

I mean as example server returns HTTP 404 - Not found error with custom HTML page. That HTML will be in Error Stream not in Input Stream.

Of course response code is in connection.getResponseCode() as int (i.e. 404) and response message is in getResponseMessage() (i.e. "Not Found")