gpt4 book ai didi

c# - 为什么 Request.QueryString.Get() 和 NameValueCollection.Get() 返回解码值?

转载 作者:太空宇宙 更新时间:2023-11-03 12:18:23 24 4
gpt4 key购买 nike

我在从查询字符串中提取值的几行代码中观察到一些奇怪/意外的行为。

函数调用

ValidateRequestNameValueCollection(HttpContext.Current.Request.Headers // other params omitted

函数定义

 private void ValidateRequestNameValueCollection(NameValueCollection nvc, // other params omitted
{
int count = nvc.Count;

validationFailureIndex = 0;

for (int index = 0; index < count; ++index)
{
string key = nvc.GetKey(index);

if (null != key && key.StartsWith(DoubleUnderline, StringComparison.Ordinal))
continue;

string str = nvc.Get(index); // this is returning a decoded string

在 visual studio 调试器中,如果我将鼠标悬停在 NameValueCollection 参数上,我会看到与此类似的内容:

{param=omitted3GCDqHrqg5w%2b6NJfc%3d}

这是明确编码的,但是当 .get 被调用时,返回的字符串被解码。基于此处的答案:HttpValueCollection and NameValueCollection我认为 NameValueCollection 参数被静默转换为 HttpValueCollection 并且 HttpValueCollection 在获取时自动调用 UrlDecode。不过,我找不到任何明确说明这一点的文档。有人知道吗?

最佳答案

参见 https://referencesource.microsoft.com/#System.Web/HttpValueCollection.cshttps://github.com/Microsoft/referencesource/blob/master/System.Web/HttpValueCollection.cs

internal void FillFromString(String s, bool urlencoded, Encoding encoding) {
...
if (urlencoded)
base.Add(
HttpUtility.UrlDecode(name, encoding),
HttpUtility.UrlDecode(value, encoding));
else
...

关于c# - 为什么 Request.QueryString.Get() 和 NameValueCollection.Get() 返回解码值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48670131/

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