gpt4 book ai didi

c# - 如何用所有请求 header 填充字典

转载 作者:太空狗 更新时间:2023-10-29 20:56:13 26 4
gpt4 key购买 nike

当我使用此方法获得 header 键名时,我能够一个一个地获取请求 header

private string GetHeader(string Name)
{
IEnumerable<string> headerValues;
if (Request.Headers.TryGetValues(Name, out headerValues))
{
return headerValues.FirstOrDefault();
}
else { return ""; }
}

但我真正想要的是获取所有请求 header 并将它们存储在字典中,就像这样

Dictionary<string, string> ss = Request.Headers.ToDictionary(a => a.Key, a => a.Value); 
//this doesn't work

有人知道怎么做吗?

最佳答案

您已经可以枚举所有 header 值,因为 Request.Headers类型 HttpRequestHeadersIEnumerable<KeyValuePair<string, IEnumerable<string>>> .

因此值是 IEnumerable<string>和你的 Dictionary必须更改为

Dictionary<string, IEnumerable<string>> ss 
= Request.Headers.ToDictionary(a => a.Key, a => a.Value);

或者如果你想坚持使用字符串值,你可以加入字符串枚举。

Dictionary<string, string> ss 
= Request.Headers.ToDictionary(a => a.Key, a => string.Join(";", a.Value));

关于c# - 如何用所有请求 header 填充字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39525694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com