capcapdk
capcapdk

Reputation: 179

C# Method to URL encode data to '%xx'

I am in a situation where i have to replace some Windows-1252 special characters. More characters may be added later - so i want a general method instead of this hardcoding.

private string URLEncode(string s)
        {
            s = s.Replace("ø", "%f8");
            s = s.Replace("Ø", "%f8");
            s = s.Replace("æ", "%e6");
            s = s.Replace("Æ", "%e6");
            s = s.Replace("å", "%e5");
            s = s.Replace("Å", "%e5");
            return s;
        }

Is this possible? I don't know what this '%f8' format is, but it works.

Upvotes: 0

Views: 210

Answers (1)

Fabio Gariglio
Fabio Gariglio

Reputation: 91

Okay, now I get your problem. I think I found a suitable solution in another post:

UrlEncoding issue for string with ß character

Which in your case could be:

var source = "tØst".ToLowerInvariant();
var encodedUrl = HttpUtility.UrlEncode(source, Encoding.GetEncoding(1252));

I had to make the string lowercase to better match your current mapping.

Upvotes: 2

Related Questions