如何在Web Client类中获取所有标题,包括发布数据?

时间:2011-08-05 21:05:42

标签: c# .net asp.net request-headers

我正在寻找一个代码示例,如何在我的Web应用程序中获取所有收到的标题,包括发布数据。

某事:

String headers = client.Headers.ToString(); 
Response.Write(headers); 

输出:

       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: keep-alive
        foo: baa
        Pragma: no-cache


      *the post data*

有可能吗?

2 个答案:

答案 0 :(得分:1)

Request.Headers不会包含发布的数据。发布的数据可以通过Request.Form对象访问,如下所示:

 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...
 }

答案 1 :(得分:0)

您可能想要Request.ServerVariables属性。你可以像这样循环:

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

以“HEADER_”开头的是原始HTTP标头。有关详细信息,请参阅MSDN的IIS Server variables documentation