Search code examples
c#httpwebresponse

Get content of HttpWebResponse while debugging


I'm trying to run sequential requests to a web api url every 10 seconds to log changes in the data returned. The code snippet looks like this:

using (Stream objStream = response.GetResponseStream())
{
    query result = (query)serializer.Deserialize(objStream);
    Console.WriteLine(result.results.quote.Name + " " + result.results.quote.Ask);
    objStream.Flush();
    objStream.Close();
}

Every now and then an InvalidOperationException is thrown when running the deserialiation with the message saying that the XML document is badly formated. In an effort to isolate the problem I'm trying to find the "raw" response content in debug mode using the autos/locals/watch view, but I really can't find it.

I can find the response header and a lot of other information and as far as I can see this looks okay with one exception; the content-length which shows -1. I'm not sure if this is something that I should care about really but since I can't find the response "body" I can't help being suspicious about it.

So my real question here is: how can I find the "body" inside a HttpWebResponse or Stream object?

And the side question: Is the content-length with value -1 something to be bothered about.


Solution

  • If you read the entire contents from the stream and store it in a variable before deserializing it, you should be able to see the contents while debugging