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

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...