首页 > 解决方案 > 如何使用服务器端代码从 http post header 获取表单数据

问题描述

我收到来自 MS 登录的 azure Active Directory 的回调。帖子在标题中包含一个 id 令牌,需要将其读入服务器端的字符串变量。

我可以在 network/headers/formdata 下的 chrome 开发工具中看到数据。我已经遍历了标题集合 - 它不存在。你到底是如何访问这些数据的?

用于显示标题的代码(实际上使用 vb.net,但现在它只占市场的一小部分,因为我正在为 c# 发布,如果需要,我可以很容易地翻译它。):

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)
    txtOutput.Text += "Key: " & arr1(loop1) & vbCrLf
    arr2 = coll.GetValues(loop1)
    ' Get all values under this key.
    For loop2 = 0 To arr2.GetUpperBound(0)
        txtOutput.Text += "Value " & CStr(loop2) & ": " & Server.HtmlEncode(arr2(loop2)) & vbCrLf & vbCrLf
    Next loop2
Next loop1

我期待在 headers 集合中找到这些数据,但它不存在。

标签: c#asp.nethttpwebforms

解决方案


你几乎拥有它。答案就在你的问题中。使用coll = Request.Form而不是coll = Request.Headers.


推荐阅读