4

In java how do you know whether you have an error stream from a Http(s)connection or if it is an InputStream? The only way I can tell to do it is go for both, check for null and catch any exceptions.

    HttpConnection con = (HttpConnection)URL.openConnection();
    //Write to output
    InputStream in = con.GetInputStream();
    //Vs
    InputStream error = con.getErrorStream();

How does java determine which stream it has? Is it based solely on response code of the connetion? So if its >=200 and <300 then its inputStream otherwhise its an errorStream?

Thanks.

2 Answers 2

5

HTTP_INTERNAL_ERROR (500) isn't the only response code that can create an error stream, there are many others: 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 501, 502, 503, 504, 505, etc.

Not only that, but connection.getResponseCode() may throw an exception if it initiated the connection and the HTTP response status code was an error-class status code. So checking for 500 (HTTP_INTERNAL_ERROR) immediately after connection.getResponseCode() may actually be unreachable code, depending on how you're accessing connection.

The strategy I have seen implemented is to use the error stream if an exception was thrown, otherwise use the input stream. The following code provides a basic structural starting point. You'll probably want to add to it.

InputStream responseStream = null;
int responseCode = -1;
IOException exception = null;
try
{
    responseCode = connection.getResponseCode();  
    responseStream = connection.getInputStream();
}
catch(IOException e)
{
    exception = e;
    responseCode = connection.getResponseCode();  
    responseStream = connection.getErrorStream();    
}

// You can now examine the responseCode, responseStream, and exception variables
// For example:

if (responseStream != null)
{
    // Go ahead and examine responseCode, but
    // always read the data from the responseStream no matter what
    // (This clears the connection for reuse).
    // Probably log the exception if it's not null
}
else
{
    // This can happen if e.g. a malformed HTTP response was received
    // This should be treated as an error.  The responseCode variable
    // can be examined but should not be trusted to be accurate.
    // Probably log the exception if it's not null
}
1

You can do it as following:

InputStream inStream = null;  
int responseCode = connection.getResponseCode();  
if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {            
    inStream = connection.getErrorStream();    
}  
else{  
    inStream = connection.getInputStream();  
}  

The HTTP return code signifies what is the kind of stream to read back.

5
  • I thought that it had to do with the response code but I wasn't so sure. Thanks.
    – Rcunn87
    Feb 27, 2012 at 20:35
  • user384706's answer doesn't handle all cases. HTTP_INTERNAL_ERROR (500) isn't the only response code that can create an error stream. Also, HttpURLConnection likes to throw an exception when the response code is 4xx or 5xx. See my answer for more details.
    – Mike Clark
    Feb 27, 2012 at 20:58
  • I was just noticing that as I was looking into it more.
    – Rcunn87
    Feb 27, 2012 at 21:17
  • @Rcunn87:+1 from me for your answer.But I said in the OP The HTTP return code signifies what is the kind of stream to read back.Perhaps I should be more clear that you should take into account other codes as well since only 2xx signifies success
    – Cratylus
    Feb 27, 2012 at 21:58
  • Well couldn't I do if(200<=responseCode && responseCode <=299)
    – Rcunn87
    Feb 28, 2012 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.