title: Retrieving server side HTTP error messages in Java
body: <p>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.</p>
  
  <pre><code> System.out.println("Response Code : " + connection.getResponseCode());
   System.out.println("Response Message : " + connection.getResponseMessage());
  </code></pre>
  
  <p>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.</p>
  
  <p><strong>Edit:</strong> This is the error I'm getting with a 403, in the python example, it returned a custom response</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>This is the code I'm using to build the response</p>
  
  <pre><code>    StringBuilder response;
      try (BufferedReader in = new BufferedReader(
              new InputStreamReader(connection.getInputStream()))) {
          String inputLine;
          response = new StringBuilder();
          while ((inputLine = in.readLine()) != null) {
              response.append(inputLine);
          }
      }
  </code></pre>