title: How do I read a HTTP response if the server returned a 502 error?
body: <p>I used <code>URLConnection</code> to send a POST request to a server and I am using the following code to  read the response:</p>
  
  <pre><code>String line;
  BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));   
  while ((line = reader.readLine()) != null) {
      response += line;
  }
  reader.close();
  </code></pre>
  
  <p>However, the server will sometimes return a 502 error with a meaningful error message in the response, which I would like to obtain. My problem is that attempting to create the <code>BufferedReader</code> in that case will result in a <code>java.io.IOException</code>:</p>
  
  <pre><code>Server returned HTTP response code: 502 for URL: &lt;url&gt;
  </code></pre>
  
  <p>How can I bypass this?</p>