gb2312提交的url编码转换成utf8的查询
使用场景,当一网站是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的查询的更多相关文章
- 将NSString转换成UTF8编码的NSString
在使用网络地址时,一般要先将url进行encode成UTF8格式的编码,否则在使用时可能报告网址不存在的错误,这时就需要进行转换 下面就是转换函数: NSString *urlString= [NSS ...
- iOS-将NSString转换成UTF8编码的NSString
在使用网络地址时,一般要先将url进行encode成UTF8格式的编码,否则在使用时可能报告网址不存在的错误,这时就需要进行转换 下面就是转换函数: NSString *urlString= [NSS ...
- GB2312转换成UTF-8与utf_8转换成GB2312
本文转载:http://www.cnblogs.com/jonhson/archive/2010/08/10/1796536.html /// <summary> /// utf_8转换成 ...
- ASP:GB2312格式文本文件转换成UTF-8格式
'-------------------------------------------------'函数名称:gb2utf_file'作用:利用AdoDb.Stream对象来把GB2312格式文本文 ...
- 转换编码,将Unicode编码转换成可以浏览的utf-8编码
//转换编码,将Unicode编码转换成可以浏览的utf-8编码 public function unicodeDecode($name) { $pattern = '/([\w]+)|(\\\u([ ...
- Linux中将一个GBK编码的文件转换成UTF-8编码文件
Linux中将一个GBK编码的文件转换成UTF-8编码文件 使用iconv 命令iconv -f GBK -t UTF-8 file1 -o file2 输出另一个文件,然后再覆盖源文件内容
- 中文的csv文件的编码改成utf-8的方法
直奔主题:把包含中文的csv文件的编码改成utf-8的方法: https://stackoverflow.com/questions/191359/how-to-convert-a-file-to-u ...
- LoadRunner 如何将英文的字符串转换成UTF-8格式的字符串?
7.48 如何手动转换字符串编码 1.问题提出 如何将英文的字符串转换成UTF-8格式的字符串? 2.问题解答 可以使用lr_convert_string_encoding函数将字符串从一种编码手动 ...
- 将string转换成UTF8在进行请求
在请求服务器时,如果参数中带有中文字符.就会报参数格式错误,需要将其转换成UTF8 @interface NSString (NSURLUtilities) /* Adds all percent e ...
随机推荐
- Week 1 工程表格
PSP2.1 Personal Software Process Stages Time Planning 计划 · Estimate · 估计这个任务需要多少时间 6h30min Developme ...
- 《Linux内核设计与实现》第七章读书笔记
第七章.中断和中断处理 7.1中断 中断使得硬件得以发出通知给处理器.中断随时可以产生,内核随时可能因为新来到的中断而被打断. 不同的设备对应的中断不同,而每个中断都通过一个唯一的数字标志.操作系统给 ...
- myeclipse从SVN检出项目报错
今天是在公司实习的第一天,从SVN服务器检出项目后发现报错. 解决方法: 1. 右键项目,选择属性properties-->选择resource-->将others选中并换为UTF-8 2 ...
- text3
GitHub地址https://github.com/gaodejian/gaodejian/blob/master/firework 课题研究的目的和意义 java编程语言在编程方面的具体应用,以及 ...
- Voltage Keepsake CodeForces - 801C (思维+二分)
题目链接 这是一道很棒的二分题. 思路: 首先先思考什么情况下是可以无限的使用,即输出-1. 我们思考可知,如果每一秒内所有设备的用电量总和小于等于充电器每秒可以充的电,那么这一群设备就可以无限使用. ...
- Expanded encryption and decryption signature algorithm SM2 & SM3
Expanded encryption and decryption signature algorithm supports multiple signature digest algorithms ...
- Oracle18c OnlyExadataVersion 安装说明
1.根据惜分飞还有盖国强老师云和恩墨的文章, 验证了下OnlyExadataVersion的Oracle18c的数据库安装过程. 2.oracle参数修改以及创建用户, 目录, 修改.bash_pro ...
- linux_查看磁盘与目录容量
一.查看磁盘容量命令df(report file system disk space usage) 终端运行 $ df 输出结果 我的物理主机上的 /dev/sda5 是对应着主机硬盘的分区,字母 a ...
- NodeJS中的require和import
ES6标准发布后,module成为标准,标准的使用是以export指令导出接口,以import引入模块,但是在我们一贯的node模块中,我们采用的是CommonJS规范,使用require引入模块,使 ...
- ssh 将22端口换为其它 防火墙设置
废话不多说,先通过当前的SSH端口(默认为:22)登陆. 1.修改配置文件:/etc/ssh/sshd_config ,找到 #port 22 2.先将Port 22 前面的 # 号去掉,并另起一行. ...