对于具有ß字符的字符串的UrlEncoding问题 [英] UrlEncoding issue for string with ß character
问题描述
当我对这个字符串进行编码时,我期望这个:%DF
,而是我得到:%c3%9f
以下是我一直用于测试的一行C#
string test = HttpUtility.UrlEncode(ß);
这是因为UrlEncode的默认实现是基于UTF8字符编码。实际上这完全在你的控制之内。
例如,以下代码:
string sample = new string((char)0x0DF,1);
string test = HttpUtility.UrlEncode(sample);
Console.WriteLine(UTF8 Ecoded:{0},test);
test = HttpUtility.UrlEncode(sample,Encoding.GetEncoding(1252));
Console.WriteLine(1252 Ecoded:{0},test);
输出以下内容:
UTF8 Ecoded:%c3%9f
1252 Ecoded:%df
当然,在URI上使用其他编码的危险是某些字符不能被表示...例如,这个代码:
>
string sample = new string((char)312,1);
Encoding encoding = Encoding.GetEncoding(1252);
string test = HttpUtility.UrlEncode(sample);
Console.WriteLine(UTF8 Ecoded:{0},round-trip = {1},test,sample == HttpUtility.UrlDecode(test));
test = HttpUtility.UrlEncode(sample,encoding);
Console.WriteLine(1252 Ecoded:{0},round-trip = {1},test,sample == HttpUtility.UrlDecode(test,encoding));
Console.ReadLine();
将输出以下内容:
UTF8 Ecoded:%c4%b8,round-trip = True
1252 Ecoded:%3f,round-trip = False
您可以在后面的示例中看到编码是%3f,当未编码等于一个问号?时,不是312的输入字符
简而言之,将ß编码为%c3%9f没有错,相反,这是正确的表示。但是,如果您必须使用远程服务器的编码%DF来正确解码,则使用1252代码页如图所示。
I have a parameter which I must pass as part of a url. The parameter contains this character: ß
When I encode this string, I am expecting this: %DF but instead i'm getting: %c3%9f
Here is a line of C# which I have been using to test
string test = HttpUtility.UrlEncode("ß");
This is because the default implementation of UrlEncode is based on the UTF8 character encoding. Actually this is entirely within your control.
For example, the following code:
string sample = new string((char)0x0DF, 1);
string test = HttpUtility.UrlEncode(sample);
Console.WriteLine("UTF8 Ecoded: {0}", test);
test = HttpUtility.UrlEncode(sample, Encoding.GetEncoding(1252));
Console.WriteLine("1252 Ecoded: {0}", test);
Outputs the following:
UTF8 Ecoded: %c3%9f
1252 Ecoded: %df
Of course the danger with using another encoding on a URI is that some characters can not be represented at all...
for example, this code:
string sample = new string((char) 312, 1);
Encoding encoding = Encoding.GetEncoding(1252);
string test = HttpUtility.UrlEncode(sample);
Console.WriteLine("UTF8 Ecoded: {0}, round-trip = {1}", test, sample == HttpUtility.UrlDecode(test));
test = HttpUtility.UrlEncode(sample, encoding);
Console.WriteLine("1252 Ecoded: {0}, round-trip = {1}", test, sample == HttpUtility.UrlDecode(test, encoding));
Console.ReadLine();
Will output the following:
UTF8 Ecoded: %c4%b8, round-trip = True
1252 Ecoded: %3f, round-trip = False
You can see in the later example the encoding is "%3f" which, when unencoded is equal to a question mark "?", not the input character of 312 (0x138).
In a nutshell there is nothing wrong with encoding "ß" as "%c3%9f", to the contrary, it is the correct representation. Yet if you must have the encoding "%DF for the remote server to correctly decode it, then use the 1252 codepage as shown.
这篇关于对于具有ß字符的字符串的UrlEncoding问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!