Пример #1
0
        public static string PathSegmentEncode(string str, Encoding e)
        {
            var pathEncoded = new StringBuilder();
            var characters  = new CharacterEnumerator(str);

            while (characters.MoveNext())
            {
                var character = characters.Current;
                if (Regex.IsMatch(character, @"^[A-Za-z0-9\-._~!$&'()*+,;=:@]$"))
                {
                    pathEncoded.Append(character);
                }
                else
                {
                    var bytes = e.GetBytes(character);
                    foreach (var b in bytes)
                    {
                        pathEncoded.Append('%');
                        pathEncoded.Append(b.ToString("X2"));
                    }
                }
            }

            return(pathEncoded.ToString());
        }
Пример #2
0
        public static string Encode(string value, Encoding encoding)
        {
            var uriEncoding = new StringBuilder();
            var characters  = new CharacterEnumerator(value);

            while (characters.MoveNext())
            {
                if (characters.Current.Length == 1 && Unreserved.Contains(characters.Current[0]))
                {
                    uriEncoding.Append(characters.Current);
                }
                else
                {
                    foreach (var b in encoding.GetBytes(characters.Current))
                    {
                        uriEncoding.Append('%');
                        uriEncoding.Append(b.ToString("X2"));
                    }
                }
            }

            return(uriEncoding.ToString());
        }