[英]HttpUtility.UrlEncode with uppercase hex characters
HttpUtility.UrlEncode("abc : 123")
produces abc+%3a+123
but I need it to produce abc+%3A+123
(notice the uppercase A
. HttpUtility.UrlEncode("abc : 123")
产生abc+%3a+123
但我需要它来产生abc+%3A+123
(注意大写A
Is there a way to have UrlEncode output uppercase hex characters? 有没有办法让UrlEncode输出大写十六进制字符?
I don't want to simply call .ToUpper()
on the entire string because I need abc
to stay lowercase. 我不想简单地在整个字符串上调用.ToUpper()
,因为我需要abc
保持小写。
This is not supported out of the box, but you can do it yourself: 开箱即用不支持此功能,但是您可以自己执行以下操作:
private static string UrlEncodeUpperCase(string stringToEncode)
{
var reg = new Regex(@"%[a-f0-9]{2}");
stringToEncode = HttpUtility.UrlEncode(stringToEncode);
return reg.Replace(stringToEncode, m => m.Value.ToUpperInvariant());
}
Keep in mind that [RFC 3986][1]
explicitly mentions that uppercase and lowercase are equivalent. 请记住, [RFC 3986][1]
明确提到大写和小写是等效的。
I believe I've found the answer from the 2nd answer in this question: https://stackoverflow.com/a/1148326/20471 我相信我已经在这个问题的第二个答案中找到了答案: https : //stackoverflow.com/a/1148326/20471
The solution is to use Uri.EscapeDataString
because it uses %20
for spaces instead of +
and also uppercases the hex replacements. 解决方案是使用Uri.EscapeDataString
因为它将%20
用作空格而不是+
,并且将十六进制替换大写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.