gb2312提交的url编码转换成utf8的查询

时间:2021-03-06 03:20:25

使用场景,当一网站是gb2312的编码向另一个是utf8的网站提交查询

如:http://search.chinayq.com/?key=%C0%D6%C6%F7

其中key为gb2312的url编码

可以自动转换成utf8解码后的汉字

/// <summary>

/// 判断是否是utf8编码

/// </summary>

/// <param name="str"></param>

/// <returns></returns>

private static bool IsUTF8(byte[] buf)         {

int i;

byte cOctets; // octets to go in this UTF-8 encoded character

bool bAllAscii = true;

long iLen = buf.Length;

cOctets = 0;

for (i = 0; i < iLen; i++)             {

if ((buf[i] & 0x80) != 0) bAllAscii = false;

if (cOctets == 0)                 {

if (buf[i] >= 0x80)                     {                         do                         {                             buf[i] <<= 1;                             cOctets++;                         }

while ((buf[i] & 0x80) != 0);

cOctets--;

if (cOctets == 0)

return false;

}

}

else                 {

if ((buf[i] & 0xC0) != 0x80)

return false;

cOctets--;

}

}

if (cOctets > 0)

return false;

if (bAllAscii)

return false;

return true;

}

/// <summary>         /// url转换为字节         /// </summary>

/// <param name="url"></param>

/// <returns></returns>

private static byte[] GetUrlCodingToBytes(string url)         {

StringBuilder sb = new StringBuilder();

int i = url.IndexOf('%');

while (i >= 0)             {

if (url.Length < i + 3)                 {

break;

}

sb.Append(url.Substring(i, 3));

url = url.Substring(i + 3);

i = url.IndexOf('%');

}

string urlCoding = sb.ToString();

if (string.IsNullOrEmpty(urlCoding))

return new byte[0];

urlCoding = urlCoding.Replace("%", string.Empty);

int len = urlCoding.Length / 2;

byte[] result = new byte[len];

len *= 2;

for (int index = 0; index < len; index++)             {

string s = urlCoding.Substring(index, 2);

int b = int.Parse(s, System.Globalization.NumberStyles.HexNumber);

result[index / 2] = (byte)b;

index++;

}

return result;

}

/// <summary>

/// Request得到string类型

/// </summary>

/// <param name="name">参数</param>

/// <param name="sum">最大字符数</param>

/// <returns></returns>

public static string GetString( string name, int sum )         {

HttpRequest request = System.Web.HttpContext.Current.Request;

if (request[name] != null )             {

byte[] url = GetUrlCodingToBytes(request.Url.PathAndQuery);

string str = request[name];

string getStr = Regex.Match(request.Url.Query, "" + name + @"=([\s\S]+)&?$").Groups[1].Value;

if (!IsUTF8(url) && getStr!="")             {

str = HttpUtility.UrlDecode(getStr, System.Text.ASCIIEncoding.GetEncoding("gb2312"));             }             else             {

str = HttpUtility.UrlDecode(str);             }

if (str.Length > sum)                 {

str = str.Substring(0, sum);                 }

return FiltrateDangerCharacter(str);             }

else             {                 return string.Empty;             }         }