xmlHttpRequest 以Post方式发数据到Asp.net页,在gb2312编码下的解决办法
2008-10-21 11:30
621 查看
首先xmlHttpRequest 使用Post时,需要对数据进行编码,在客户端一般使用js中的encodeURIComponent
在web.config中指定了gb2312编码后,在aspx页面中如果直接使用 Request[xxx]那么结果将会出现乱码,
原因是asp.net系统使用gb2312编码对上传的数据进行解码还原,而encodeURIComponent编码是按uft-8来的.
为了避免这个问题,我们需要见xmlHttpRequest发送上来的原始数据(字节)按utf-8进行解码处理,
方式一
代码如下
浏览器端(js):
function myXMLHttpRequest(){
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlHttp = false;
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}
return xmlHttp;
}
var xmlHttp=myXMLHttpRequest();
content = "user="+encodeURIComponent("大发啊个啊按时法米]]吗吗吗*-234^342942023&^+//");
content +="&data=" +encodeURIComponent("到这里结束了!");
xmlHttp.Open("POST", "doc.aspx", false);
xmlHttp.setRequestHeader("Content-Length",content.length);
xmlHttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
xmlHttp.Send(content);
服务器端(asp.net)
Byte[] bytes= Request.BinaryRead(Request.ContentLength);
NameValueCollection req= FillFromEncodedBytes(bytes, Encoding.UTF8);
Response.Write(req["User"]);
其中FillFromEncodedBytes定义如下(这个是ms内部代码,通过reflector 获得,作用就是根据url编码将字段跟数据分解出来)
略有改动
private NameValueCollection FillFromEncodedBytes(byte[] bytes, Encoding encoding)
{
NameValueCollection _form = new NameValueCollection();
int num = (bytes != null) ? bytes.Length : 0;
for (int i = 0; i < num; i++)
{
string str;
string str2;
int offset = i;
int num4 = -1;
while (i < num)
{
byte num5 = bytes[i];
if (num5 == 0x3d)
{
if (num4 < 0)
{
num4 = i;
}
}
else if (num5 == 0x26)
{
break;
}
i++;
}
if (num4 >= 0)
{
str = HttpUtility.UrlDecode(bytes, offset, num4 - offset, encoding);
str2 = HttpUtility.UrlDecode(bytes, num4 + 1, (i - num4) - 1, encoding);
}
else
{
str = null;
str2 = HttpUtility.UrlDecode(bytes, offset, i - offset, encoding);
}
_form.Add(str, str2);
if ((i == (num - 1)) && (bytes[i] == 0x26))
{
_form.Add(null, string.Empty);
}
}
return _form;
}
参考代码:http://files.cnblogs.com/wdfrog/xmlhttp.rar
---------------------------------------------------------
方式二,使用GET方式发来的请求,上面(方式一)的每个字段也可以使用下面方式进行转换
客户端:(使用Utf-8编码方式)
/services/regServices.aspx?username='+encodeURI(un.value)
服务端:(默认情况下,Request["xxx"]使用gb2312进行UrlDecode处理,所以将结果按gb2312 UrlEncode,后再使用utf-8进行UrlDecode)
string data1 = Util.GetQ("username", "");
string data2= HttpUtility.UrlEncode(data1, Encoding.GetEncoding("GB2312"));
string username = HttpUtility.UrlDecode(data2, Encoding.UTF8);
方式三,处理方式类似第一种,因为要获取的Get,中的Query
IServiceProvider provider = (IServiceProvider)HttpContext.Current;
HttpWorkerRequest wr= provider.GetService(typeof(HttpWorkerRequest)) as HttpWorkerRequest;
byte[] bytes = wr.GetQueryStringRawBytes();
NameValueCollection req = FillFromEncodedBytes(bytes, Encoding.UTF8);
string u = req["username"];
google_ad_client = "pub-9768854185654179";
/* 468x60, 创建于 09-2-12 */
google_ad_slot = "6840815711";
google_ad_width = 468;
google_ad_height = 60;
相关文章推荐
- xmlHttpRequest 以Post方式发数据到Asp.net页,在gb2312编码下的解决办法
- HttpWebRequest Post 方式发送 XML数据
- httpclient post方式服务端获取不到数据的解决办法
- 原始ajax通过xmlHttpRequest对象的send()方法提交数据--Get方式和Post方式
- http协议4---GET方式和POST方式获取表单数据举例3(统一方式获取,解决中文乱码问题)
- 解析HTTP POST方式请求的xml数据、本地XML文件数据, 并返回JSON字符串
- 操作XML 报错:根级别上的数据无效 和 给定编码中的字符无效 解决办法
- 后端接收不到AngularJs中$http.post发送的数据原因分析及解决办法
- 微信post xml数据获取不了的解决办法
- 通过httpwebrequest实现http的post和get方法莫名出现500内部服务器错误的解决办法
- XMLHttpRequest的POST方式发送
- Java 通过HttpURLConnection Post方式提交xml,并从服务端返回数据
- nodejs的http.request使用post方式提交数据请求
- XMLHttpRequest读取中文网页时返回乱码的解决办法
- ajax中XMLHttpRequest对象的open()方法GET和POST方式区别
- XMLHttpRequest读取中文网页时返回乱码的解决办法
- Chrome 下,重复使用 XMLHttpRequest进行Post数据时,遇到一个奇怪的问题
- Java 通过httpClient Post方式提交xml,并从服务端返回数据
- webrequest post 数据 编码 HttpWebRequest get
- 解决办法:Synchronous XMLHttpRequest on the main thread is deprecated because of its