HttpUitlity.UrlDecode(string)如何工作?

时间:2015-05-04 09:53:04

标签: c# urldecode

我正在使用HttpUtility.UrlDecode(string)。但是,当我尝试解码字符串"%ab"时,它返回“ ”字符,这会产生问题。

3 个答案:

答案 0 :(得分:1)

如果查看此link,您可以看到您可以将编码作为参数发送到函数。我会玩这个,很可能,你从函数得到的字符串编码不是UTF-8。

答案 1 :(得分:1)

https://msdn.microsoft.com/en-us/library/adwtk1fy(v=vs.110).aspx

将已编码以便在URL中传输的字符串转换为已解码的字符串。

网址编码参考:http://www.w3schools.com/tags/ref_urlencode.asp

它很可能是您尝试解码的UTF8网址,并且%ab%不会引用任何内容 - 这就是您获得“'�&#39”的原因;-字符。它不知道将这个解码为什么字符。

如果你尝试解码这样的东西:'这个%20is%20a%20text'它将返回:'这是一个文本'因为%20 ='空格' -character

答案 2 :(得分:0)

您可以找到方法on the reference source page的实现,它实质上对指定的URL执行每字符验证,并根据需要进行转换。

您现在面临的问题很可能与输出字符串的编码有关。 UrlDecode返回的字符可能会返回您正在显示字符串的编码不支持的字符,从而产生“怪异”字符。

为了完整起见,这是整个方法:

    internal string UrlDecode(string value, Encoding encoding) {
        if (value == null) {
            return null;
        }

        int count = value.Length;
        UrlDecoder helper = new UrlDecoder(count, encoding);

        // go through the string's chars collapsing %XX and %uXXXX and
        // appending each char as char, with exception of %XX constructs
        // that are appended as bytes

        for (int pos = 0; pos < count; pos++) {
            char ch = value[pos];

            if (ch == '+') {
                ch = ' ';
            }
            else if (ch == '%' && pos < count - 2) {
                if (value[pos + 1] == 'u' && pos < count - 5) {
                    int h1 = HttpEncoderUtility.HexToInt(value[pos + 2]);
                    int h2 = HttpEncoderUtility.HexToInt(value[pos + 3]);
                    int h3 = HttpEncoderUtility.HexToInt(value[pos + 4]);
                    int h4 = HttpEncoderUtility.HexToInt(value[pos + 5]);

                    if (h1 >= 0 && h2 >= 0 && h3 >= 0 && h4 >= 0) {   // valid 4 hex chars
                        ch = (char)((h1 << 12) | (h2 << 8) | (h3 << 4) | h4);
                        pos += 5;

                        // only add as char
                        helper.AddChar(ch);
                        continue;
                    }
                }
                else {
                    int h1 = HttpEncoderUtility.HexToInt(value[pos + 1]);
                    int h2 = HttpEncoderUtility.HexToInt(value[pos + 2]);

                    if (h1 >= 0 && h2 >= 0) {     // valid 2 hex chars
                        byte b = (byte)((h1 << 4) | h2);
                        pos += 2;

                        // don't add as char
                        helper.AddByte(b);
                        continue;
                    }
                }
            }

            if ((ch & 0xFF80) == 0)
                helper.AddByte((byte)ch); // 7 bit have to go as bytes because of Unicode
            else
                helper.AddChar(ch);
        }

        return Utf16StringValidator.ValidateString(helper.GetString());
    }