By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,671 Members | 1,393 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,671 IT Pros & Developers. It's quick & easy.

Getting HTTP Message from HttpWebResponse

P: n/a
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.
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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:c.*********************@no-sp-am-eh-mail.com
remove all "no-sp-am-eh"s to reply
"C# Learner" <cs****@learner.here> wrote in message
news:s0********************************@4ax.com...
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.


Nov 15 '05 #2

P: n/a
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:c.*********************@no-sp-am-eh-mail.com
remove all "no-sp-am-eh"s to reply

"Cezary Nolewajka" <c.*********************@no-sp-am-eh-mail.com> wrote in message news:uc**************@TK2MSFTNGP09.phx.gbl...
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:c.*********************@no-sp-am-eh-mail.com
remove all "no-sp-am-eh"s to reply


"C# Learner" <cs****@learner.here> wrote in message
news:s0********************************@4ax.com...
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.

Nov 15 '05 #3

P: n/a
"Cezary Nolewajka" <c.*********************@no-sp-am-eh-mail.com>
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
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.