我想准备一个字符串,将其放入“post”请求中。不幸的是,我发现编码url的所有方法似乎只对少数几个字符应用了百分比编码。各种方法,例如。 HttpUtility.UrlEncode
,留下一些字符,例如()和§不变。
答案 0 :(得分:4)
这更像是你在寻找什么?
string input = @"such as () and § untouched.";
//Console.WriteLine(input);
Console.WriteLine(HttpUtility.UrlEncodeUnicode(input));
Console.WriteLine(HttpUtility.UrlEncode(input));
string everything = string.Join("", input.ToCharArray().Select(c => "%" + ((int)c).ToString("x2")).ToArray());
Console.WriteLine(everything);
Console.WriteLine(HttpUtility.UrlDecode(everything));
//This is my understanding of what you're asking for:
string everythingU = string.Join("", input.ToCharArray().Select(c => "%u" + ((int)c).ToString("x4")).ToArray());
Console.WriteLine(everythingU);
Console.WriteLine(HttpUtility.UrlDecode(everythingU));
输出:
such+as+()+and+%u00a7+untouched.
such+as+()+and+%c2%a7+untouched.
%73%75%63%68%20%61%73%20%28%29%20%61%6e%64%20%a7%20%75%6e%74%6f%75%63%68%65%64%2e
such as () and � untouched.
%u0073%u0075%u0063%u0068%u0020%u0061%u0073%u0020%u0028%u0029%u0020%u0061%u006e%u0064%u0020%u00a7%u0020%u0075%u006e%u0074%u006f%u0075%u0063%u0068%u0065%u0064%u002e
such as () and § untouched.
答案 1 :(得分:0)
不幸的是,大多数现有转换器只查看不兼容的URL字符。主要用于(如您所述)URL编码,或用于防止跨站点脚本编写。
如果你想从头开始制作,需要一些时间来进行查找,但这会很有趣。您可以覆盖现有编码器并添加与您有关的其他字符,也可以更改所有字母和数字。
以下是UTF / ASCII的良好链接 - > HTML编码(%)