Web乱码折腾够呛 小小妙招轻松搞定

开发 前端
最近被乱码折腾的够呛,现在工作告一段落,出来总结一下Web中传递数据乱码的情况,希望同样被乱码困扰的朋友能够安心入睡!

Web数据提交有两种方法:GET 和 POST。关于这两种方法的介绍,请看这里:Http之Get/Post请求区别。我在这里要介绍的是如何在程序中获取HTTPRequest数据,并成功解决编码不同时所引起乱码的问题。

现在我们开始,先看一段HTML代码:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. <html xmlns="http://www.w3.org/1999/xhtml"> 
  4. <head> 
  5. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  6. <title>无标题文档</title> 
  7. </head> 
  8.  
  9. <body> 
  10.     <form id="myForm" action="http://localhost:9000/WebForm1.aspx" method="post"> 
  11.         名称:<input tyep="text" name="name" width="200px" value="独钓寒江"/> 
  12.         <br /> 
  13.         年龄:<input tyep="text" name="age" width="200px" value="24"/> 
  14.         <br /> 
  15.         <br /> 
  16.         <input type="submit" value="提交" /> 
  17.     </form> 
  18. </body> 
  19. </html> 
  20.  

在这个HTML文件中,我们使用的编码是GB2312,Form表单中包含name和age两个数据。首先将method设置为GET方法:

  1. <form id="myForm" action="http://localhost:9000/WebForm1.aspx" method="GET"> 

另外我们再新建一个Web应用程序,并在本地新建一个站点,将端口设置为9000,添加一个页面,名称为WebForm1.aspx,也就是上面Form表单中的action所指向的地址http://localhost:9000/WebForm1.aspx

在点击“提交”按钮的时候,我们可以在WebForm1中获取到网页的参数,具体有如下几种方式:

  1. Request["name"]  
  2. Request.Params["name"]  
  3. Request.QueryString["name"] 

这三种方法得到的字符串都是经过默认编码转换的,因为我们使用vs建立项目时编码默认为UTF-8,所以这时便会出现乱码。这是***种问题,稍候我们将解决这个问题。

接下来将method设置为POST方法:

  1. <form id="myForm" action="http://localhost:9000/WebForm1.aspx" method="POST"> 

在点击“提交”按钮的时候,我们可以在WebForm1中获取到网页的参数,具体有如下几种方式:

  1. Request["name"]  
  2. Request.Params["name"]  
  3. Request.Form["name"] 

和***种问题相同,经过默认的UTF-8转换,这里还会出现乱码。这是第二种问题。

问题一的解决方法:

  1. StringBuilder sb = new StringBuilder();  
  2. IServiceProvider provider = (IServiceProvider)HttpContext.Current;  
  3. HttpWorkerRequest worker = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));  
  4. byte[] bs = worker.GetQueryStringRawBytes();  
  5. String queryString = Encoding.GetEncoding("GB2312").GetString(bs);  
  6. NameValueCollection querys = HttpUtility.ParseQueryString(queryString, Encoding.GetEncoding("GB2312"));  
  7.  
  8. foreach (var item in querys.Keys)  
  9. {  
  10.     sb.AppendFormat("{0}:{1}<br />", item.ToString(), querys[item.ToString()]);  

问题二的解决方法:

  1. // 获取到InputStream  
  2. System.IO.Stream str = Request.InputStream;  
  3. Int32 strLen, strRead;  
  4. strLen = Convert.ToInt32(str.Length);  
  5. byte[] strArr = new byte[strLen];  
  6. strstrRead = str.Read(strArr, 0, strLen);  
  7.               
  8. string queryString = HttpUtility.UrlDecode(strArr, System.Text.Encoding.GetEncoding("GB2312"));  
  9.  
  10. NameValueCollection querys = HttpUtility.ParseQueryString(queryString, Encoding.GetEncoding("GB2312"));  
  11.  
  12. foreach (var item in querys.Keys)  
  13. {  
  14.     sb.AppendFormat("{0}:{1}<br />", item.ToString(), querys[item.ToString()]);  

另外,对于***种方法,还可以直接将URL用GB2312解码,这里不再贴出代码。

有了这两种方法,不管是怎样的乱码,都可以高枕无忧了。

原文链接:http://www.cnblogs.com/youring2/archive/2011/03/24/1993717.html

【编辑推荐】

  1. Web前端研发工程师编程能力飞升之路
  2. 为什么IE9是Web设计师的噩梦
  3. 从Web技术看开源是否有利可图?
  4. Web开发ABC:初学者必知的26个概念和技术
  5. Web开发者欣喜若狂的20款Chrome拓展
责任编辑:陈贻新 来源: 快乐的企鹅
相关推荐

2010-07-21 11:50:24

telnet乱码

2009-12-11 15:37:58

Linux日志处理

2017-05-11 15:01:43

Androidweb布局

2011-05-05 14:51:52

一体机

2010-06-03 10:26:29

开发MySQL中文乱码

2009-01-03 08:58:00

2010-06-04 09:08:56

2009-11-12 10:53:57

ADO.NET连接My

2009-09-13 20:28:38

Linq插入数据

2010-07-27 14:25:02

linux文件编码

2022-09-16 08:04:25

阿里云权限网络

2011-05-12 10:54:58

一体机技巧

2016-03-17 17:35:15

云容器虚拟化管理Docker

2011-05-23 14:15:31

2009-08-17 08:45:34

Windows 7文件删除

2009-11-24 15:34:41

DNS服务器组建

2022-09-29 10:51:18

ShellLinux命令审计

2009-10-23 17:51:51

Oracle用户密码

2010-09-17 14:04:14

JVM内存设置

2009-11-26 16:30:52

Suse中文乱码问题
点赞
收藏

51CTO技术栈公众号