1

Is there a way to extract the text that is being sent back as part of the WebException that occurs can occur with a HttpWebResponse? I can get all of the header information but there is a custom message being returned with the 400 or 401 response that I would like to get if possible. I am currently handling the exception in my test like this:

        var ex = Assert.Throws<WebException>(() =>
        {
            HttpWebResponse response = Utils.GetRawResponse(url);
        });

        Assert.Contains("401", ex.Message);

Here is how am getting the response:

public static HttpWebResponse GetRawResponse(string requestURL)
{

    HttpWebRequest request = WebRequest.Create(requestURL) as HttpWebRequest;

    HttpWebResponse response = request.GetResponse() as HttpWebResponse;


    return response;

}

And this works, but does not have the custom message.

Top hopefully be a little more clear am am referring to the message text in the bottom of the screenshot.: enter image description here

Dan Snell
  • 2,185
  • 3
  • 21
  • 35
  • That message should be in the response body, have you tried to get the response stream and read it to a string? – Gusman Apr 24 '14 at 23:42
  • Ah stream the exception response... duh. – Dan Snell Apr 24 '14 at 23:59
  • 1
    In the end, a 400 response is a normal web response, inside a WebException there is a Response object with the real response from the server – Gusman Apr 25 '14 at 00:03
  • Yup I saw that and just didn't even think about getting the response. – Dan Snell Apr 25 '14 at 00:11
  • possible duplicate of [HttpWebResponse returns 404 error](http://stackoverflow.com/questions/1857512/httpwebresponse-returns-404-error) – Adam Maras Apr 25 '14 at 00:20
  • @AdamMaras - this is not a precise duplicate. Related perhaps. I was asking how to get the message out of the exception response, not how to handle the exception. Thanks. – Dan Snell Apr 25 '14 at 03:03

2 Answers2

2

With Gusmans reminder I created a method to extract the response from the WebException:

    public static string ParseExceptionRespose(WebException exception)
    {
        string responseContents;
        Stream descrption = ((HttpWebResponse)exception.Response).GetResponseStream();

        using (StreamReader readStream = new StreamReader(descrption))
        {
            responseContents = readStream.ReadToEnd();
        }

        return responseContents;

    }
Dan Snell
  • 2,185
  • 3
  • 21
  • 35
1

When handling the exception, try something like:

if (ex.Response.ContentLength > 0) 
{
    string ResponseBody = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
    // Do whatever you want with ResponseBody
}

Similar to examples in http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.contentlength(v=vs.110).aspx)

The key point being that WebException had a Response property: http://msdn.microsoft.com/en-us/library/system.net.webexception.response(v=vs.110).aspx

Powkachu
  • 2,170
  • 2
  • 26
  • 36
RickH
  • 2,416
  • 16
  • 17