Getting HTTP Message from HttpWebResponse

  • Thread starter Thread starter C# Learner
  • Start date Start date
C

C# Learner

The following code gets the *body* of an HTTP message:

<code>

response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(),
Encoding.UTF8);
string html = sr.ReadToEnd();

</code>

Instead of the body, I want the full *message*.

Example:

HTTP/1.1 200 OK [rest of header...] [body...]

Is it possible to get this "raw" message? By *message* I mean header
+ body.

Thanks in advance.
 
Yo can get the pieces of the information using the following members of the
HttpWebResponse:

ProtocolVersion - protocol version (HTTP/1.1 bit)
StatusCode - the HTTP Response status (the "200 OK" and others)
Headers - the headers of the response (the headers: Content-Encoding,
Content-Length, Content-Type, Last-Modified, Server)
Cookies - the cookies values
....

So you can get all the information you have in the header.

If you want to get the whole response as is, you can use sockets and get the
whole response into a byte array and convert it to a string.
 
OK. I found the solution. The point is to read the standard input stream (Console.In) but only, if the enfironmental variable "CONTENT_LENGTH" shows that some data has been post.

So, if you prepare a form like this:

<HTML>
<FORM action="http://localhost/testapps/cgi/cgitest.exe" method=POST>
<input type=text name=textfield value=test />
<input type=submit value="Submit"/>
</form>
</HTML>

This is ok. But if you ommit the name of a field like:

<HTML>
<FORM action="http://localhost/testapps/cgi/cgitest.exe" method=POST>
<input type=text value=test />
<input type=submit value="Submit"/>
</form>
</HTML>

the values are not post. That is why the stream might be empty.

The code to read POST data would look like:

str = Environment.GetEnvironmentVariable ("CONTENT_LENGTH");
if (str != "0" && str != "")
{
char[] chBuff = new Char [Convert.ToInt32 (str, 10)];
Console.In.Read (chBuff, 0, Convert.ToInt32 (str, 10));
string strPostData = new StringBuilder ().Append (chBuff).ToString ();
Console.WriteLine ("POST DATA: " + strPostData + "<BR>");
}

Please, forgive me the way the output data is being constructed but just wanted to show how one can get the data.

--
Cezary Nolewajka
mailto:[email protected]
remove all "no-sp-am-eh"s to reply

Cezary Nolewajka said:
Yo can get the pieces of the information using the following members of the
HttpWebResponse:

ProtocolVersion - protocol version (HTTP/1.1 bit)
StatusCode - the HTTP Response status (the "200 OK" and others)
Headers - the headers of the response (the headers: Content-Encoding,
Content-Length, Content-Type, Last-Modified, Server)
Cookies - the cookies values
...

So you can get all the information you have in the header.

If you want to get the whole response as is, you can use sockets and get the
whole response into a byte array and convert it to a string.

--
Cezary Nolewajka
mailto:[email protected]
remove all "no-sp-am-eh"s to reply


C# Learner said:
The following code gets the *body* of an HTTP message:

<code>

response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(),
Encoding.UTF8);
string html = sr.ReadToEnd();

</code>

Instead of the body, I want the full *message*.

Example:

HTTP/1.1 200 OK [rest of header...] [body...]

Is it possible to get this "raw" message? By *message* I mean header
+ body.

Thanks in advance.
 
"Cezary Nolewajka" <[email protected]>
wrote:

<snip>

Hi, thanks for the replies. I'm looking into something else now (as
detailed in the thread I started entitled "[OT] .NET Rant".

Regards
 
Back
Top