Q:

String Encoding Umlaute

I want to send a string with a webclient and i have problems with german umlaute.
Checking with Fiddler, I see that the following conversion for the post parameter is made:
ä to %E4 (which is iso-8859-1 according to online encoder tool)
Do you know how i can achive this for WP7?
I tried already:

string urlstring = HttpUtility.UrlEncode("ä"); 
RESULT: %c3%a4
string urlstring1 = HttpUtility.HtmlEncode("ä"); 
RESULT: ä
Encoding isoenc = Encoding.GetEncoding("ISO-8859-1");
byte[] utf8characters = Encoding.UTF8.GetBytes("ä");
byte[] isoArray = Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding("ISO-8859-1"),utf8characters);
string finalString = isoenc.GetString(isoArray,0, isoArray.Length); 
RESULT: ä

I also checked this silverlight tool where you can create custom encoding.. without success.
I'm getting really crazy about this issue, if anyone has an idea please let me know.
Thanks, 
 Ralf

A:

The solution provided by nwellnhof is good, but the HttpUtility.UrlEncode overload that takes an encoding as parameter isn't available on Windows Phone. Fortunately, by decompiling the framework assemblies, it's easy to modify it to use the encoding you want:
public class HttpUtilityEx
{
    public static string UrlEncode(string url, Encoding encoding)
    {
        if (url == null)
        {
            return null;
        }
        byte[] bytes = encoding.GetBytes(url);
        int num = 0;
        int num1 = 0;
        int length = (int)bytes.Length;
        for (int i = 0; i < length; i++)
        {
            char chr = (char)bytes[i];
            if (chr == ' ')
            {
                num++;
            }
            else if (!IsSafe(chr))
            {
                num1++;
            }
        }
        if ((num != 0 ? true : num1 != 0))
        {
            byte[] hex = new byte[length + num1 * 2];
            int num2 = 0;
            for (int j = 0; j < length; j++)
            {
                byte num3 = bytes[j];
                char chr1 = (char)num3;
                if (IsSafe(chr1))
                {
                    int num4 = num2;
                    num2 = num4 + 1;
                    hex[num4] = num3;
                }
                else if (chr1 != ' ')
                {
                    int num5 = num2;
                    num2 = num5 + 1;
                    hex[num5] = 37;
                    int num6 = num2;
                    num2 = num6 + 1;
                    hex[num6] = (byte)IntToHex(num3 >> 4 & 15);
                    int num7 = num2;
                    num2 = num7 + 1;
                    hex[num7] = (byte)IntToHex(num3 & 15);
                }
                else
                {
                    int num8 = num2;
                    num2 = num8 + 1;
                    hex[num8] = 43;
                }
            }
            bytes = hex;
        }
        return encoding.GetString(bytes, 0, (int)bytes.Length);
    }

    private static bool IsSafe(char ch)
    {
        if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9')
        {
            return true;
        }
        char chr = ch;
        if (chr != '!')
        {
            switch (chr)
            {
                case '\'':
                case '(':
                case ')':
                case '*':
                case '-':
                case '.':
                    {
                        break;
                    }
                case '+':
                case ',':
                    {
                        return false;
                    }
                default:
                    {
                        if (chr != '\u005F')
                        {
                            return false;
                        }
                        else
                        {
                            break;
                        }
                    }
            }
        }
        return true;
    }

    internal static char IntToHex(int n)
    {
        if (n <= 9)
        {
            return (char)(n + 48);
        }
        return (char)(n - 10 + 97);
    }
}

From there, you just need to call it like you would for HttpUtility:
var result = HttpUtilityEx.UrlEncode("ä", Encoding.GetEncoding("ISO-8859-1"));

