Reputation: 81
I would like to prepare a string for to put it into a "post" request. Unfortunately all the methods I found to encode an url seem to apply percent-encoding only to a handful of characters. The various methods, eg. HttpUtility.UrlEncode
, leave some characters, such as () and § untouched.
Upvotes: 3
Views: 5246
Reputation: 1490
Unfortunately, most existing converters only look at the non-compatible URL characters. Mainly for (as you mentioned) URL encoding, or for the prevention of cross site scripting.
If you want to make one from scratch, it would take some time to do the look-up, but it would be interesting. You could override existing encoders and add the additional characters that concern you, or change all letters and numbers too.
Here is a good link for UTF/ASCII --> HTML Encoded (%)
Upvotes: 0
Reputation: 9195
Is this more what you're looking for?
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));
which outputs:
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.
Upvotes: 5