Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows phone 7 字符串编码Umlaute_Windows Phone 7_Encoding_Utf 8_Urlencode - Fatal编程技术网

Windows phone 7 字符串编码Umlaute

Windows phone 7 字符串编码Umlaute,windows-phone-7,encoding,utf-8,urlencode,Windows Phone 7,Encoding,Utf 8,Urlencode,我想发送一个带有webclient的字符串,但我遇到了德语umlaute的问题。 通过使用Fiddler进行检查,我看到对post参数进行了以下转换: ä至%E4(根据在线编码器工具,这是iso-8859-1) 你知道我怎样才能为WP7做到这一点吗 我已经试过了: string urlstring=HttpUtility.UrlEncode(“ä”) 结果:%c3%a4 string urlstring1=HttpUtility.HtmlEncode(“ä”) 结果:ä Encoding-is

我想发送一个带有webclient的字符串,但我遇到了德语umlaute的问题。 通过使用Fiddler进行检查,我看到对post参数进行了以下转换:

ä至%E4
(根据在线编码器工具,这是iso-8859-1)

你知道我怎样才能为WP7做到这一点吗

我已经试过了:

  • string urlstring=HttpUtility.UrlEncode(“ä”)

    结果:
    %c3%a4

  • string urlstring1=HttpUtility.HtmlEncode(“ä”)

    结果:
    ä

  • Encoding-isonc=Encoding.GetEncoding(“ISO-8859-1”)
    byte[]utf8characters=Encoding.UTF8.GetBytes(“ä”)
    byte[]isoArray=Encoding.Convert(Encoding.UTF8,Encoding.GetEncoding(“ISO-8859-1”),UTF8字符)
    
    string finalString=isonc.GetString(isoArray,0,isoArray.Length)

    结果:
    ä

我还检查了这个silverlight工具,您可以在其中创建自定义编码。。没有成功

我对这个问题非常感兴趣,如果有人有什么想法,请告诉我

谢谢,
Ralf

HttpUtility.UrlEncode
具有指定编码的。以下方面应起作用:

Encoding isoenc = Encoding.GetEncoding("ISO-8859-1");
String urlstring = HttpUtility.UrlEncode("ä", isoenc);

nwellnhof提供的解决方案很好,但是以编码为参数的
HttpUtility.UrlEncode
重载在Windows Phone上不可用。幸运的是,通过反编译框架程序集,可以很容易地修改它以使用您想要的编码:

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);
    }
}

这是一个有趣的问题。您可以通过执行以下操作来检索E4:
Encoding.GetEncoding.GetBytes(“iso-8859-1”).GetBytes(“ä”)[0].ToString(“X”)
,从那里,您可以使用自定义字符串编码器检查大于127的字节,并用其编码值替换它们?另外,是否确实需要发送
%E4
?问题可能只是您发送到服务器的值没有在iso-8859-1中编码,因此我再次检查了没有编码的值:我发送了一个包含“ä”的字符串请求将被拒绝,我发送了一个包含“%E4”的字符串,它是从服务器接收的,并显示“ä”。因此我再次检查了没有编码的值:我发送了一个包含“ä”的字符串请求将被拒绝;我发送了一个包含“%E4”的字符串,它被服务器接受并显示为“ä”,当发送“%c3%a4”(标准HttpUtility.Encoding)时,它显示为“Ô(顺便说一句。我还尝试了webclient.Encoding iso-8859-1,但这将不起作用,因为该字符串有多个参数(带=),webclient.encoding utf8很好,在我通过网页发表文章时也会显示在Fiddler中),但您提到的方法是一种有趣的方法,我将对此进行研究。Thx:-)不幸的是,这个重载在Windows Phone上不可用,这正是我想要的。你救了我一天。非常感谢:-)。。。我现在已经把你的博客加入书签了。
var result = HttpUtilityEx.UrlEncode("ä", Encoding.GetEncoding("ISO-8859-1"));