How get all headers including post data in web Client class?
I'm looking for an code example how get all received headers in my web application including the post data.
something as:
String headers = client.Headers.ToString();
Response.Write(headers);
Output:
POST http://localhost:52133/test/Default.aspx HTTP/1.1
Host: localhost:52133
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:5.0.1) Gecko/20100101 Firefox/5.0.1
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: kee开发者_开发问答p-alive
foo: baa
Pragma: no-cache
*the post data*
Is it possible?
Request.Headers will not include the posted data. The posted data can be accessed via Request.Form object like the following:
for(int i = 0; i < Request.Form.Count; i++)
{
string key = Request.Form.GetKey(i);
string value = Request.Form[i];
// now do something with the key-value pair...
}
You probably want the Request.ServerVariables
property. You can loop through it like this:
Dim loop1, loop2 As Integer
Dim arr1(), arr2() As String
Dim coll As NameValueCollection
' Load ServerVariable collection into NameValueCollection object.
coll=Request.ServerVariables
' Get names of all keys into a string array.
arr1 = coll.AllKeys
For loop1 = 0 To arr1.GetUpperBound(0)
Response.Write("Key: " & arr1(loop1) & "<br>")
arr2 = coll.GetValues(loop1) ' Get all values under this key.
For loop2 = 0 To arr2.GetUpperBound(0)
Response.Write("Value " & CStr(loop2) & ": " & Server.HtmlEncode(arr2(loop2)) & "<br>")
Next loop2
Next loop1
The ones that start with "HEADER_" are the raw HTTP headers. See MSDN's IIS Server variables documentation for more info.
精彩评论