gpt4 book ai didi

c# - QueryString 真的不区分大小写吗?

转载 作者:太空狗 更新时间:2023-10-29 23:30:17 25 4
gpt4 key购买 nike

我在一个支付平台上工作,为了响应支付,一个简单的 GET 调用,在查询字符串中有一些参数,是对我的听众的:

http://localhost/mytest/listener?TIMECREATED=04.08.2015+12%3A22%3A27&statoattuale=OK&PREVIOUSSTATE=IN&CURRENTSTATE=payment_approved&tipomessaggio=PAYMENT_STATE&DESCRIZIONE=CAMBIO+DI+STATO&datacreazione=04.08.2015+12%3A22%3A27&stabilimento=xxxxxx&MerchantNumber=xxxxxx&descrizione=CAMBIO+DI+STATO&OBJECT=PAYMENT&TIMEGENERATED=04.08.2015+12%3A23%3A17&MERCHANTNUMBER=xxxxxx&statoprecedente=IN&MERCHANTACCOUNT=xxxxxx&numeroOrdine=myOrderNo&numeroCommerciante=xxxxxx&datagenerazione=04.08.2015+12%3A23%3A17&ORDERNUMBER=myOrderNo&Stabilimento=xxxxxx&mac=CaWJiRCxbWH%2FsNFMvHUD2A%3D%3D&MAC=AnsEvRHkvMwRL%2FgehVtnhA%3D%3D

当我检查 Request.QueryString 时,我得到的是乱七八糟的参数顺序和大小写。似乎它们是在第一次出现时用调整后的大小写重新排序的。像这样:

TIMECREATED=04.08.2015 12:22:27&statoattuale=OK&PREVIOUSSTATE=IN&CURRENTSTATE=payment_approved&tipomessaggio=PAYMENT_STATE&DESCRIZIONE=CAMBIO DI STATO&DESCRIZIONE=CAMBIO DI STATO&datacreazione=04.08.2015 12:22:27&stabilimento=xxxxxx&stabilimento=xxxxxx&MerchantNumber=xxxxxx&MerchantNumber=xxxxxx&OBJECT=PAYMENT&TIMEGENERATED=04.08.2015 12:23:17&statoprecedente=IN&MERCHANTACCOUNT=999988801&numeroOrdine=myOrderNo&numeroCommerciante=xxxxxx&datagenerazione=04.08.2015 12:23:17&ORDERNUMBER=myOrderNo&mac=CaWJiRCxbWH/sNFMvHUD2A==&mac=AnsEvRHkvMwRL/gehVtnhA==

对我来说这看起来像是一个错误,因为 RFC3986 指出:

When a URI uses components of the generic syntax, the component syntax equivalence rules always apply; namely, that the scheme and host are case-insensitive and therefore should be normalized to lowercase. For example, the URI is equivalent to http://www.example.com/. The other generic syntax components are assumed to be case-sensitive unless specifically defined otherwise by the scheme (see Section 6.2.3).

目前我通过手动解析 Url.Query 解决了我的问题,但我仍然认为 Request.QueryString 的行为方式不正确。

有人可以阐明这件事吗?

最佳答案

不幸的是,API 没有提供使 Request.QueryString 集合区分大小写(或 Request.HeadersRequest.Form 集合,就此而言)。

然而,通过反射进行一些逆向工程,这并不难。

public class CaseSensitiveQueryStringCollection : System.Collections.Specialized.NameValueCollection
{
public CaseSensitiveQueryStringCollection(string queryString, bool urlencoded, System.Text.Encoding encoding)
// This makes it case sensitive, the default is StringComparer.OrdinalIgnoreCase
: base(StringComparer.Ordinal)
{
if (queryString.StartsWith("?"))
{
queryString = queryString.Substring(1);
}

this.FillFromString(queryString, urlencoded, encoding);
}

internal void FillFromString(string s, bool urlencoded, System.Text.Encoding encoding)
{
int num = (s != null) ? s.Length : 0;
for (int i = 0; i < num; i++)
{
int startIndex = i;
int num4 = -1;
while (i < num)
{
char ch = s[i];
if (ch == '=')
{
if (num4 < 0)
{
num4 = i;
}
}
else if (ch == '&')
{
break;
}
i++;
}
string str = null;
string str2 = null;
if (num4 >= 0)
{
str = s.Substring(startIndex, num4 - startIndex);
str2 = s.Substring(num4 + 1, (i - num4) - 1);
}
else
{
str2 = s.Substring(startIndex, i - startIndex);
}
if (urlencoded)
{
base.Add(HttpUtility.UrlDecode(str, encoding), HttpUtility.UrlDecode(str2, encoding));
}
else
{
base.Add(str, str2);
}
if ((i == (num - 1)) && (s[i] == '&'))
{
base.Add(null, string.Empty);
}
}
}
}

用法

var query = new CaseSensitiveQueryStringCollection(
HttpContext.Current.Request.Url.Query,
true,
System.Text.Encoding.UTF8);

当您使用像 ?MAC=123&mac=456 这样的查询字符串时,您可以看到它们是分开的。

关于c# - QueryString 真的不区分大小写吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31813904/

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