Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The response to my HTTP request returned with a status code 404, which resulted in a FileNotFoundException when I called getInputStream(). I still wanted to read the response body, so I had to use a different method: <strong>HttpURLConnection#getErrorStream()</strong>.</p> <p>Here's a JavaDoc snippet of getErrorStream():</p> <blockquote> <p>Returns the error stream if the connection failed but the server sent useful data nonetheless. The typical example is when an HTTP server responds with a 404, which will cause a FileNotFoundException to be thrown in connect, but the server sent an HTML help page with suggestions as to what to do.</p> </blockquote> <p>Usage example:</p> <pre><code>public static String httpGet(String url) { HttpURLConnection con = null; InputStream is = null; try { con = (HttpURLConnection) new URL(url).openConnection(); con.connect(); //4xx: client error, 5xx: server error. See: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html. boolean isError = con.getResponseCode() &gt;= 400; //In HTTP error cases, HttpURLConnection only gives you the input stream via #getErrorStream(). is = isError ? con.getErrorStream() : con.getInputStream(); String contentEncoding = con.getContentEncoding() != null ? con.getContentEncoding() : "UTF-8"; return IOUtils.toString(is, contentEncoding); //Apache Commons IO } catch (Exception e) { throw new IllegalStateException(e); } finally { //Note: Closing the InputStream manually may be unnecessary, depending on the implementation of HttpURLConnection#disconnect(). Sun/Oracle's implementation does close it for you in said method. if (is != null) { try { is.close(); } catch (IOException e) { throw new IllegalStateException(e); } } if (con != null) { con.disconnect(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload