コード例 #1
0
        public static string UrlEncode(string s, Encoding Enc)
        {
            if (s == null)
            {
                return(null);
            }
            if (s == string.Empty)
            {
                return(string.Empty);
            }
            bool flag   = false;
            int  length = s.Length;

            for (int i = 0; i < length; i++)
            {
                char c = s[i];
                if (((((c < '0') || ((c < 'A') && (c > '9'))) || ((c > 'Z') && (c < 'a'))) || (c > 'z')) && !HttpEncoder.NotEncoded(c))
                {
                    flag = true;
                    break;
                }
            }
            if (!flag)
            {
                return(s);
            }
            byte[] bytes = new byte[Enc.GetMaxByteCount(s.Length)];
            int    count = Enc.GetBytes(s, 0, s.Length, bytes, 0);

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