Search code examples
c#httpresponsewebrequest

How to get the response stream on a non 201 status code


I'm having some problem's grabbing a response stream from a request that returning status code 422.

HttpWebResponse objResponse = (HttpWebResponse)wr.GetResponse();

The wr.GetResponse throws an webexception because the status code isnt 201.
So i can never get the response stream from the remote server. But it does return important information in the ResponseStream.

So my question is how to retrieve the ResponseStream on a request that doesnt return 201 but still returns data.


Solution

  • The WebException that is thrown contains a reference to the response from which you can get the response steam.

    catch(WebException ex)
    {
         ex.Response.GetResponseStream();
    }