问题
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("ß");
回答1:
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.
回答2:
The ß
character is encoded as %c3%9f
when using the UTF-8 encoding. This is what you should be using, if possible.
If your target webserver uses some other encoding, you need to know exactly what encoding that is. Since you want to encode that character into %df
that could be Windows-1252 or Windows-1250 (or possibly others).
If you're sure this is what you want to do, you can use (assuming Windows-1252):
HttpUtility.UrlEncode("ß", Encoding.GetEncoding(1252))
来源:https://stackoverflow.com/questions/10405671/urlencoding-issue-for-string-with-%c3%9f-character