Exemplo n.º 1
0
        public static string UrlDecode(string s, Encoding encoding)
        {
            if (s == null || s.Length == 0 || !s.Contains(new char[]
            {
                '%',
                '+'
            }))
            {
                return(s);
            }
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }
            int         length = s.Length;
            List <byte> list   = new List <byte>();

            for (int i = 0; i < length; i++)
            {
                char c = s[i];
                if (c == '%' && i + 2 < length && s[i + 1] != '%')
                {
                    int @char;
                    if (s[i + 1] == 'u' && i + 5 < length)
                    {
                        @char = HttpUtility.GetChar(s, i + 2, 4);
                        if (@char != -1)
                        {
                            HttpUtility.WriteCharBytes(list, (char)@char, encoding);
                            i += 5;
                        }
                        else
                        {
                            HttpUtility.WriteCharBytes(list, '%', encoding);
                        }
                    }
                    else if ((@char = HttpUtility.GetChar(s, i + 1, 2)) != -1)
                    {
                        HttpUtility.WriteCharBytes(list, (char)@char, encoding);
                        i += 2;
                    }
                    else
                    {
                        HttpUtility.WriteCharBytes(list, '%', encoding);
                    }
                }
                else if (c == '+')
                {
                    HttpUtility.WriteCharBytes(list, ' ', encoding);
                }
                else
                {
                    HttpUtility.WriteCharBytes(list, c, encoding);
                }
            }
            byte[] bytes = list.ToArray();
            return(encoding.GetString(bytes));
        }