Link to home
Start Free TrialLog in
Avatar of mrichmon
mrichmon

asked on

View Actual Response Stream of a HttpWebRequest

I am working with System.Net.HttpWebRequest objects and would like to see the actual response stream.

The catch - our servers require SSL - so intercepting does not work as we see the response, but it is encrypted.

I have tried the following:

using (StreamReader streamReader = new StreamReader(request.GetResponse()))
{
       Response.Write(streamReader.ReadToEnd());
}

The result is:
https://myserver.com/exchange/mrichmon/Calendar/testappointment.emlHTTP/1.1 200 OK

However if I have code similar to this:

using(XmlReader reader = XmlReader.Create(request.GetResponseStream()))
{
      reader.MoveToContent();
      while(reader.Read())
      {
            if(reader.NodeType == XmlNodeType.Element)
            {
                  Response.Write("&lt;" + reader.Name + "&gt;<br>");
                  Response.Write(reader.ReadElementString());
            }
      }
}

Then I get results like this:
<a:response>
https://myserver.com/exchange/mrichmon/Calendar/testappointment.eml<a:propstat>
HTTP/1.1 200 OK<a:contentclass>
<c:subject>
<d:location>
<d:dtend>
<d:busystatus>
<d:alldayevent>
<d:reminderoffset>

So I know there actually is xml content there.

So, the question is how do I view the actual response so that I can look at the xml, see the defined namespaces, see the fields returned, etc.

Also related is what is the best way to get this xml from teh stream into a variable that can be used in C# code
Avatar of RoninThe
RoninThe

personally I prefer Read() to ReadToEnd(), nothing to do with your question here though. The problem seems to do with GetResponse() and GetResponseStream().
try this-

string respString = "";
using( StreamReader readStream = new StreamReader( request.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8"))
{
      Char[] read = new Char[256];
      int count = readStream.Read( read, 0, 256 );
      while (count > 0)
      {
            // Dump the 256 characters on a string.
            String str = new String(read, 0, count);
            respString += str;
            count = readStream.Read(read, 0, 256);
      }
}
Console.Write( respString);
if( respString != "")
{
      XmlDocument doc = new XmlDocument();
      try
      {
            doc.LoadXml( respString);
      }
      catch( XmlException e)
      {
      }
}
Avatar of mrichmon

ASKER

No that does not work.

Instead the output is the exact same as
using (StreamReader streamReader = new StreamReader(request.GetResponse()))
{
       Response.Write(streamReader.ReadToEnd());
}

which is https://myserver.com/exchange/mrichmon/Calendar/testappointment.emlHTTP/1.1 200 OK
Okay I figured it out.

Very simple solution - I am suprised no one mentioned it.

The problem was that since the output was going through a Response.Write - it was in the source, but only the above text was being displayed to the screen.

Simple huh :o)
ASKER CERTIFIED SOLUTION
Avatar of GhostMod
GhostMod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial