如何获取 Web Client 类中的所有标头(包括发布数据)?

发布于 2024-11-28 04:47:34 字数 541 浏览 2 评论 0原文

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

如下:

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*

这可能吗?

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


      *the post data*

Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青衫负雪 2024-12-05 04:47:35

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

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...
 }
一笑百媚生 2024-12-05 04:47:35

您可能需要 < code>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 服务器变量文档

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文