title: HttpURLConnection: how to read payload of 400 response
body: <p>I use the following code to send a JSON request to my web service, and, for a bad request I return a 400 message code along with a detailed JSON error response in the payload.</p>
  
  <p>I do not understand how I could possibly retrieve this payload on the client side when using <code>HttpURLConnection</code>, as it immediately throws an <code>IOException</code> and the connection's <code>InputStream</code> is <code>null</code>.</p>
  
  <pre><code>HttpURLConnection connection = this.connect(url);
  connection.setRequestMethod(method);
  OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
  out.write(jsonMessage);
  InputStream is = null;
  try {
      is = connection.getInputStream(); // IOException is thrown, how can I still get payload??
  } catch (Exception e) {
  } 
  String response = getStringFromInputStream(is);
  </code></pre>
  
  <p>Also tried to use <code>getErrorStream()</code>, but this was containing Apache Tomcat's default error page rather than the payload I can see when, e.g. I use a visual REST API client.</p>