Beispiel #1
0
        public static string UrlEncode(string str, Encoding e)
        {
            if (str == null)
            {
                return(null);
            }

            if (str == String.Empty)
            {
                return(String.Empty);
            }

            bool needEncode = false;
            int  len        = str.Length;

            for (int i = 0; i < len; i++)
            {
                char c = str [i];
                if ((c < '0') || (c < 'A' && c > '9') || (c > 'Z' && c < 'a') || (c > 'z'))
                {
                    if (HttpEncoder.NotEncoded(c))
                    {
                        continue;
                    }

                    needEncode = true;
                    break;
                }
            }

            if (!needEncode)
            {
                return(str);
            }

            // avoided GetByteCount call
            byte [] bytes   = new byte[e.GetMaxByteCount(str.Length)];
            int     realLen = e.GetBytes(str, 0, str.Length, bytes, 0);

            return(Encoding.ASCII.GetString(UrlEncodeToBytes(bytes, 0, realLen)));
        }