Request 接收参数乱码原理解析
起因:
今天早上被同事问了一个问题:说接收到的参数是乱码,让我帮着解决一下。
实际情景:
同事负责的平台是Ext.js框架搭建的,web.config配置文件里配置了全局为“GB2312”编码:
<globalization requestEncoding="gb2312" responseEncoding="gb2312" fileEncoding="gb2312" culture="zh-CN"/>
当前台提交“中文文字”时,后台用Request.QueryString["xxx"]接收到的是乱码。
无论用System.Web.HttpUtility.UrlDecode("xxx","编码类型")怎么解码都无效。
原理说明:
1:首先确定的是:客户端的url参数在提交时,Ext.js会对其编码再提交,而客户端的编码默认是utf-8编码
客户端默认有三种编码函数:escape() encodeURI() encodeURIComponent()
2:那为什么用Request.QueryString["xxx"]接收参数时,收到的会是乱码?
为此,我们必须解开Request.QueryString的原始处理逻辑过程
我们步步反编绎,
2.1:看QueryString属性的代码:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->public NameValueCollection QueryString
{
get
{
if (this._queryString == null)
{
this._queryString = new HttpValueCollection();
if (this._wr != null)
{
this.FillInQueryStringCollection();//重点代码切入点
}
this._queryString.MakeReadOnly();
}
if (this._flags[1])
{
this._flags.Clear(1);
ValidateNameValueCollection(this._queryString, "Request.QueryString");
}
return this._queryString;
}
}
2.2:切入 FillInQueryStringCollection()方法
private void FillInQueryStringCollection()
{
byte[] queryStringBytes = this.QueryStringBytes;
if (queryStringBytes != null)
{
if (queryStringBytes.Length != 0)
{
this._queryString.FillFromEncodedBytes(queryStringBytes, this.QueryStringEncoding);
}
}//上面是对流字节的处理,即文件上传之类的。
else if (!string.IsNullOrEmpty(this.QueryStringText))
{
//下面这句是对普通文件提交的处理:FillFromString是个切入点,编码切入点是:this.QueryStringEncoding
this._queryString.FillFromString(this.QueryStringText, true, this.QueryStringEncoding); }
}
2.3:切入:QueryStringEncoding
internal Encoding QueryStringEncoding
{
get
{
Encoding contentEncoding = this.ContentEncoding;
if (!contentEncoding.Equals(Encoding.Unicode))
{
return contentEncoding;
}
return Encoding.UTF8;
}
}
//点击进入this.ContentEncoding则为:
public Encoding ContentEncoding
{
get
{
if (!this._flags[0x20] || (this._encoding == null))
{
this._encoding = this.GetEncodingFromHeaders();
if (this._encoding == null)
{
GlobalizationSection globalization = RuntimeConfig.GetLKGConfig(this._context).Globalization;
this._encoding = globalization.RequestEncoding;
}
this._flags.Set(0x20);
}
return this._encoding;
}
set
{
this._encoding = value;
this._flags.Set(0x20);
}
}
说明:
从QueryStringEncoding代码得出,系统默认会先取globalization配置节点的编码方式,如果取不到,则默认为UTF-8编码方式
2.4:切入 FillFromString(string s, bool urlencoded, Encoding encoding)
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->internal void FillFromString(string s, bool urlencoded, Encoding encoding)
{
int num = (s != null) ? s.Length : 0;
for (int i = 0; i < num; i++)
{
int startIndex = i;
int num4 = -1;
while (i < num)
{
char ch = s[i];
if (ch == '=')
{
if (num4 < 0)
{
num4 = i;
}
}
else if (ch == '&')
{
break;
}
i++;
}
string str = null;
string str2 = null;
if (num4 >= 0)
{
str = s.Substring(startIndex, num4 - startIndex);
str2 = s.Substring(num4 + 1, (i - num4) - 1);
}
else
{
str2 = s.Substring(startIndex, i - startIndex);
}
if (urlencoded)//外面的传值默认是true,所以会执行以下语句
{
base.Add(HttpUtility.UrlDecode(str, encoding), HttpUtility.UrlDecode(str2, encoding));
}
else
{
base.Add(str, str2);
}
if ((i == (num - 1)) && (s[i] == '&'))
{
base.Add(null, string.Empty);
}
}
}
说明:
从这点我们发现:所有的参数输入,都调用了一次:HttpUtility.UrlDecode(str2, encoding);
3:结论出来了
当客户端js对中文以utf-8编码提交到服务端时,用Request.QueryString接收时,会先以globalization配置的gb2312去解码一次,于是,产生了乱码。
所有的起因为:
1:js编码方式为urt-8 2:服务端又配置了默认为gb2312 3:Request.QueryString默认又会调用HttpUtility.UrlDecode用系统配置编码去解码接收参数。
文章补充:
1:系统取默认编码的顺序为:http请求头->globalization配置节点-》默认UTF-8 2:在Url直接输入中文时,不同浏览器处理方式可能不同如:ie不进行编码直接提交,firefox对url进行gb2312编码后提交。 3:对于未编码“中文字符”,使用Request.QueryString时内部调用HttpUtility.UrlDecode后,由gb2312->utf-8时, 如果查不到该中文字符,默认转成"%ufffd",因此出现不可逆乱码。
4:解决之路
知道了原理,解决的方式也有多种多样了:
1:全局统一为UTF-8编码,省事又省心。
2:全局指定了GB2312编码时,url带中文,js非编码不可,如ext.js框架。
这种方式你只能特殊处理,在服务端指定编码解码,
因为默认系统调用了一次HttpUtility.UrlDecode("xxx",系统配置的编码),
因此你再调用一次HttpUtility.UrlEncode("xxx",系统配置的编码),返回到原始urt-8编码参数
再用HttpUtility.UrlDecode("xxx",utf-8),解码即可。
5:其它说明:默认对进行一次解码的还包括URI属性,而Request.RawUrl则为原始参数
http://www.cnblogs.com/cyq1162/archive/2010/11/29/1891124.html
Request 接收参数乱码原理解析的更多相关文章
- Request 接收参数乱码原理解析三:实例分析
通过前面两篇<Request 接收参数乱码原理解析一:服务器端解码原理>和<Request 接收参数乱码原理解析二:浏览器端编码原理>,了解了服务器和浏览器编码解码的原理,接下 ...
- Request 接收参数乱码原理解析二:浏览器端编码原理
上一篇<Request 接收参数乱码原理解析一:服务器端解码原理>,分析了服务器端解码的过程,那么浏览器是根据什么编码的呢? 1. 浏览器解码 浏览器根据服务器页面响应Header中的“C ...
- Request 接收参数乱码原理解析一:服务器端解码原理
“Server.UrlDecode(Server.UrlEncode("北京")) == “北京””,先用UrlEncode编码然后用UrlDecode解码,这条语句永远为true ...
- 详细解析ASP.NET中Request接收参数乱码原理
起因:今天早上被同事问了一个问题:说接收到的参数是乱码,让我帮着解决一下. 实际情景: 同事负责的平台是Ext.js框架搭建的,web.config配置文件里配置了全局为“GB2312”编码: < ...
- 处理request接收参数的中文乱码的问题:
Ø POST的解决方案: * POST的参数在请求体中,直接到达后台的Servlet.数据封装到Servlet中的request中.request也有一个缓冲区.request的缓冲区也是ISO-88 ...
- laravel 请求request 接收参数
获取请求输入 获取所有输入值 你可以使用 all 方法以数组格式获取所有输入值: $input = $request->all(); 获取单个输入值 使用一些简单的方法,就可以从 Illumin ...
- GET请求中的乱码原理解析和解决方案
2. 乱码问题解决 基础知识 1)浏览器会在中文的UTF-8后加上上%得到URL编码 例如: %e8%b4%b9%e7%94%a8%e6%8a%a5%e9%94%80 2)以get的请求发送到to ...
- 对小程序的网络请求的封装 wx.request 接收参数修改
wepy-mall/wxRequest.js at master · dyq086/wepy-mall https://github.com/dyq086/wepy-mall/blob/master/ ...
- SprinMVC接收参数乱码解决篇
1.Spring 默认的字符编码格式为iso-8859-1,为此Spring专门提供了字符过滤器org.springframework.web.filter.CharacterEncodingFilt ...
随机推荐
- ural 1114,计数dp
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1114 题意:N个盒子,a个红球,b个蓝球,把求放到盒子中去,没有任何限制,有多少种放法. ...
- C#实现中国天气网XML接口测试
点击链接查看中国天气网接口说明,最近想研究一下接口测试,源于最近一次和某公司的技术总监(交大校友)谈话,发现接口测试的需求是比较大的,于是想要研究一下. 好不容易在网上找到了一个关于中国天气网的接口说 ...
- 遍历对象的list删除时报错问题。
我们对一个对象的list或者map进行删除操作时,可能会这么写 for(Distributor distributor:distributorList){ String distributorShor ...
- P1119 灾后重建
题目背景 B地区在地震过后,所有村庄都造成了一定的损毁,而这场地震却没对公路造成什么影响.但是在村庄重建好之前,所有与未重建完成的村庄的公路均无法通车.换句话说,只有连接着两个重建完成的村庄的公路才能 ...
- ocruntime
原作: http://www.jianshu.com/p/25a319aee33d 三种方法的选择 Runtime提供三种方式来将原来的方法实现代替掉,那该怎样选择它们呢? Method Resolu ...
- c#存储过程
1. 只返回单一记录集的存储过程 SqlConnection sqlconn = new SqlConnection(conn); SqlCommand cmd = new SqlCo ...
- Eclipse常用快捷键windows
Ctrl+1:快速修正Ctrl+W: 关闭当前文件ctrl+O:打开outlineCtrl+D: 删除当前行 Ctrl+L: 定位在某行Ctrl+Q:转到上次修改位置Ctrl+/:注释代码Ctrl+H ...
- 获取或者设置时,无后缀和A后缀和W后缀的区别
SetWindowTextW表示设置的字符串是WCHAR (双字节字符 )SetWindowTextA表示设置的字符串是CHAR (单字节字符 )SetWindowText表示设置的字符串是自动匹配当 ...
- Spring 框架 详解 (四)------IOC装配Bean(注解方式)
Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注 ...
- spring事务管理-摘抄
原著网址 http://gcq04552015.iteye.com/blog/1666570 Spring是以代理的方式实现对事务的管理.我们在Action中所使用的Service对象,其实是代理对象 ...