Javascript 中的decodeURI和 asp.net HttpUtility.UrlEncode之间的乱码解决方案

来源:互联网 发布:淘宝店营业执照 编辑:程序博客网 时间:2024/06/05 18:27

client端不动:

server:

  public class HttpTools
{
    public HttpTools()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public static string EzUrlEncode(string str)
    {
        if (string.IsNullOrEmpty(str))
            return string.Empty;
        byte[] bytes = Encoding.UTF8.GetBytes(str);
        byte[] data = UrlEncodeBytesToBytesInternal(bytes, 0, bytes.Length, false);
        return Encoding.ASCII.GetString(data);

    }
    internal static bool IsSafe(char ch)
    {
        if ((((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z'))) || ((ch >= '0') && (ch <= '9')))
        {
            return true;
        }
        switch (ch)
        {
            case '!':
            case
'@':
            case '$':
            case '&':
            case '*':
            case '(':
            case ')':
            case '=':
            case ':':
            case '/':
            case ';':
            case '?':
            case '+':
            case '/'':
            case ',':
                return true;
        }
        return false;
    }

    internal static char IntToHex(int n)
    {
        if (n <= 9)
        {
            return (char)(n + 0x30);
        }
        return (char)((n - 10) + 0x61);
    }
    private static byte[] UrlEncodeBytesToBytesInternal(byte[] bytes, int offset, int count, bool alwaysCreateReturnValue)
    {
        int num = 0;
        int num2 = 0;
        for (int i = 0; i < count; i++)
        {
            char ch = (char)bytes[offset + i];
            if (ch == ' ')
            {
                num++;
            }
            else if (!IsSafe(ch))
            {
                num2++;
            }
        }
        if ((!alwaysCreateReturnValue && (num == 0)) && (num2 == 0))
        {
            return bytes;
        }
        byte[] buffer = new byte[count + (num2 * 2)];
        int num4 = 0;
        for (int j = 0; j < count; j++)
        {
            byte num6 = bytes[offset + j];
            char ch2 = (char)num6;
            if (IsSafe(ch2))
            {
                buffer[num4++] = num6;
            }
            else if (ch2 == ' ')
            {
                buffer[num4++] = 0x20;
            }

            else
            {
                buffer[num4++] = 0x25;
                buffer[num4++] = (byte)IntToHex((num6 >> 4) & 15);
                buffer[num4++] = (byte)IntToHex(num6 & 15);
            }
        }
        return buffer;
    }
 

 

}

 

----------------------------------


encodeURI() 方法:把URI字符串采用UTF-8编码格式转化成escape格式的字符串。不会被此方法编码的字符:! @ # $& * ( ) = : / ; ? + '

英文解释:MSDN JScript Reference: The encodeURI method returns an encoded URI. If you pass the result to decodeURI, the original string is returned. The encodeURI method does not encode the following characters: ":", "/", ";", and "?". Use encodeURIComponent to encode these characters. Edge Core Javascript Guide: Encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF- 8 encoding of the character

----------------------------------------------------------------

原创粉丝点击