1

我正在开发一个 vb.net 2.0 应用程序并尝试读取 HTTP 标头。我可以通过 Request.Headers.Get("HTTP_VARIABLE_NAME") 获取标头值。我想使用 Headers 属性获取所有标题名称/值对,并在给定页面的按钮单击事件下的单独页面上显示。

请问如何循环并写入所有名称/值对?

4

1 回答 1

0

直接取自MSDN,因此所有功劳都归功于可怜的微软员工,他被赋予了记录 system.net 命名空间的艰巨任务。虽然我确实觉得我可以自己写一个更好的例子......

以下代码示例显示 HTTP 请求中所有标头的名称和值

Dim loop1, loop2 As Integer
 Dim arr1(), arr2() As String
 Dim coll As NameValueCollection


' Load Header collection into NameValueCollection object.
coll=Request.Headers

' Put the 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

我不知道您所说的“写入页面”是什么意思,但这应该可以帮助您入门。

Is there a reason that forces you to use .NET 2? .NET 4 is still supported on Windows XP SP3 and up and offers many advantages over previous versions. Just putting it out there.

于 2012-05-15T22:57:42.080 回答