使用场景,当一网站是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;             }         }

gb2312提交的url编码转换成utf8的查询的更多相关文章

  1. 将NSString转换成UTF8编码的NSString

    在使用网络地址时,一般要先将url进行encode成UTF8格式的编码,否则在使用时可能报告网址不存在的错误,这时就需要进行转换 下面就是转换函数: NSString *urlString= [NSS ...

  2. iOS-将NSString转换成UTF8编码的NSString

    在使用网络地址时,一般要先将url进行encode成UTF8格式的编码,否则在使用时可能报告网址不存在的错误,这时就需要进行转换 下面就是转换函数: NSString *urlString= [NSS ...

  3. GB2312转换成UTF-8与utf_8转换成GB2312

    本文转载:http://www.cnblogs.com/jonhson/archive/2010/08/10/1796536.html /// <summary> /// utf_8转换成 ...

  4. ASP:GB2312格式文本文件转换成UTF-8格式

    '-------------------------------------------------'函数名称:gb2utf_file'作用:利用AdoDb.Stream对象来把GB2312格式文本文 ...

  5. 转换编码,将Unicode编码转换成可以浏览的utf-8编码

    //转换编码,将Unicode编码转换成可以浏览的utf-8编码 public function unicodeDecode($name) { $pattern = '/([\w]+)|(\\\u([ ...

  6. Linux中将一个GBK编码的文件转换成UTF-8编码文件

    Linux中将一个GBK编码的文件转换成UTF-8编码文件 使用iconv 命令iconv -f GBK -t UTF-8 file1 -o file2 输出另一个文件,然后再覆盖源文件内容

  7. 中文的csv文件的编码改成utf-8的方法

    直奔主题:把包含中文的csv文件的编码改成utf-8的方法: https://stackoverflow.com/questions/191359/how-to-convert-a-file-to-u ...

  8. LoadRunner 如何将英文的字符串转换成UTF-8格式的字符串?

    7.48  如何手动转换字符串编码 1.问题提出 如何将英文的字符串转换成UTF-8格式的字符串? 2.问题解答 可以使用lr_convert_string_encoding函数将字符串从一种编码手动 ...

  9. 将string转换成UTF8在进行请求

    在请求服务器时,如果参数中带有中文字符.就会报参数格式错误,需要将其转换成UTF8 @interface NSString (NSURLUtilities) /* Adds all percent e ...

随机推荐

  1. BugPhobia开发篇章:Beta阶段第IX次Scrum Meeting

    0x01 :Scrum Meeting基本摘要 Beta阶段第九次Scrum Meeting 敏捷开发起始时间 2015/12/25 00:00 A.M. 敏捷开发终止时间 2015/12/28 23 ...

  2. LINUX内核设计第五周——扒开系统调用的三层皮(下)

  3. pl/sql破解方法

    转载源:http://blog.csdn.net/oscar999/article/details/2123803 打开注册表在run下输入regedit删除1.HKEY_CURRENT_USER/S ...

  4. 小学四则运算APP 第三阶段冲刺-第一天

    团队成员:陈淑筠.杨家安.陈曦 团队选题:小学四则运算APP 第三次冲刺阶段时间:12.12~12.19 本次发布的是音乐播放功能,可以根据用户需求一边播放音乐一边做题,也拥有暂停播放音乐的功能,增强 ...

  5. python 中的集合set

    python中,集合(set)是一个无序排列,可哈希, 支持集合关系测试,不支持索引和切片操作,没有特定语法格式, 只能通过工厂函数创建.集合里不会出现两个相同的元素, 所以集合常用来对字符串或元组或 ...

  6. spring 注入DI

    web  项目的搭建  以注入对象的方式

  7. k8s master 节点加入到可以调配node节点中的命令

    kubectl taint nodes --all node-role.kubernetes.io/master- 应该就可以了  效果再观察 效果为

  8. [日常工作]非Windows Server 系统远程经常断以及提高性能的方法

    1. 公司内有不少windows xp windows 7 这样的操作系统的机器在机房里面用来跑自动化脚本或者是其他用处. 经常有人反馈机器过一段时间连不上, 其实这一点是一个非常小的地方 很多机器上 ...

  9. View.requestLayout

    参考:安卓高手之路之图形系统(6)requestLayout的流程 一.invalidate和postInvalidate 这两个方法都是在重绘当前控件的时候调用的.invalidate在UI线程中调 ...

  10. ElasticSearch 6.x head插件安装

    一.下载node.js yum install -y nodejs 二.安装npm npm install -g cnpm --registry=https://registry.npm.taobao ...