Exemplo n.º 1
0
        public static string UrlEncode(string s, Encoding encoding)
        {
            int length;

            if (s == null || (length = s.Length) == 0)
            {
                return(s);
            }
            bool flag = false;

            foreach (char c in s)
            {
                if (c < '0' || (c < 'A' && c > '9') || (c > 'Z' && c < 'a') || c > 'z')
                {
                    if (!HttpUtility.NotEncoded(c))
                    {
                        flag = true;
                        break;
                    }
                }
            }
            if (!flag)
            {
                return(s);
            }
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }
            byte[] bytes  = new byte[encoding.GetMaxByteCount(length)];
            int    bytes2 = encoding.GetBytes(s, 0, length, bytes, 0);

            return(Encoding.ASCII.GetString(HttpUtility.UrlEncodeToBytesInternal(bytes, 0, bytes2)));
        }
Exemplo n.º 2
0
 private static void UrlEncodeChar(char c, Stream result, bool isUnicode)
 {
     if (c > 'ÿ')
     {
         result.WriteByte(37);
         result.WriteByte(117);
         int num = (int)(c >> 12);
         result.WriteByte((byte)HttpUtility._hexChars[num]);
         num = (int)(c >> 8 & '\u000f');
         result.WriteByte((byte)HttpUtility._hexChars[num]);
         num = (int)(c >> 4 & '\u000f');
         result.WriteByte((byte)HttpUtility._hexChars[num]);
         num = (int)(c & '\u000f');
         result.WriteByte((byte)HttpUtility._hexChars[num]);
         return;
     }
     if (c > ' ' && HttpUtility.NotEncoded(c))
     {
         result.WriteByte((byte)c);
         return;
     }
     if (c == ' ')
     {
         result.WriteByte(43);
         return;
     }
     if (c < '0' || (c < 'A' && c > '9') || (c > 'Z' && c < 'a') || c > 'z')
     {
         if (isUnicode && c > '\u007f')
         {
             result.WriteByte(37);
             result.WriteByte(117);
             result.WriteByte(48);
             result.WriteByte(48);
         }
         else
         {
             result.WriteByte(37);
         }
         int num2 = (int)(c >> 4);
         result.WriteByte((byte)HttpUtility._hexChars[num2]);
         num2 = (int)(c & '\u000f');
         result.WriteByte((byte)HttpUtility._hexChars[num2]);
     }
     else
     {
         result.WriteByte((byte)c);
     }
 }