Skip to main content

Java HttpConnection/HttpsConnection input/error stream



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.


Comments

  1. user384706's answer doesn't handle all cases. 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:

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

    if (responseStream != null && responseCode != -1)
    {
    // Use the responseStream and responseCode.
    }
    else
    {
    // This can happen if e.g. a malformed HTTP response was received
    // This should be treated as an error.
    }

    ReplyDelete
  2. 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.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.