예제 #1
0
        public static string UrlEncode(string url)
        {
            if (url == null)
            {
                return(null);
            }
            byte[] array = Encoding.UTF8.GetBytes(url);
            bool   flag  = false;
            int    num   = 0;

            byte[] array2 = array;
            for (int i = 0; i < array2.Length; i++)
            {
                byte b = array2[i];
                if (b == 32)
                {
                    flag = true;
                }
                else if (!Utility._UrlEncodeIsSafe(b))
                {
                    num++;
                    flag = true;
                }
            }
            if (flag)
            {
                byte[] array3 = new byte[array.Length + num * 2];
                int    num2   = 0;
                byte[] array4 = array;
                for (int j = 0; j < array4.Length; j++)
                {
                    byte b2 = array4[j];
                    if (Utility._UrlEncodeIsSafe(b2))
                    {
                        array3[num2++] = b2;
                    }
                    else if (b2 == 32)
                    {
                        array3[num2++] = 43;
                    }
                    else
                    {
                        array3[num2++] = 37;
                        array3[num2++] = Utility._IntToHex(b2 >> 4 & 15);
                        array3[num2++] = Utility._IntToHex((int)(b2 & 15));
                    }
                }
                array = array3;
            }
            return(Encoding.ASCII.GetString(array));
        }
예제 #2
0
        public static string UrlEncode(string url)
        {
            if (url == null)
            {
                return(null);
            }
            byte[] bytes = Encoding.UTF8.GetBytes(url);
            bool   flag  = false;
            int    num   = 0;

            byte[] numArray = bytes;
            for (int i = 0; i < (int)numArray.Length; i++)
            {
                byte num1 = numArray[i];
                if (num1 == 32)
                {
                    flag = true;
                }
                else if (!Utility._UrlEncodeIsSafe(num1))
                {
                    num++;
                    flag = true;
                }
            }
            if (flag)
            {
                byte[] hex       = new byte[(int)bytes.Length + num * 2];
                int    num2      = 0;
                byte[] numArray1 = bytes;
                for (int j = 0; j < (int)numArray1.Length; j++)
                {
                    byte num3 = numArray1[j];
                    if (Utility._UrlEncodeIsSafe(num3))
                    {
                        int num4 = num2;
                        num2      = num4 + 1;
                        hex[num4] = num3;
                    }
                    else if (num3 != 32)
                    {
                        int num5 = num2;
                        num2      = num5 + 1;
                        hex[num5] = 37;
                        int num6 = num2;
                        num2      = num6 + 1;
                        hex[num6] = Utility._IntToHex(num3 >> 4 & 15);
                        int num7 = num2;
                        num2      = num7 + 1;
                        hex[num7] = Utility._IntToHex(num3 & 15);
                    }
                    else
                    {
                        int num8 = num2;
                        num2      = num8 + 1;
                        hex[num8] = 43;
                    }
                }
                bytes = hex;
            }
            return(Encoding.ASCII.GetString(bytes));
        }