自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Web亂碼折騰夠嗆 小小妙招輕松搞定

開發(fā) 前端
最近被亂碼折騰的夠嗆,現(xiàn)在工作告一段落,出來總結(jié)一下Web中傳遞數(shù)據(jù)亂碼的情況,希望同樣被亂碼困擾的朋友能夠安心入睡!

Web數(shù)據(jù)提交有兩種方法:GET 和 POST。關(guān)于這兩種方法的介紹,請(qǐng)看這里:Http之Get/Post請(qǐng)求區(qū)別。我在這里要介紹的是如何在程序中獲取HTTPRequest數(shù)據(jù),并成功解決編碼不同時(shí)所引起亂碼的問題。

現(xiàn)在我們開始,先看一段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>無標(biāo)題文檔</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="獨(dú)釣寒江"/> 
  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.  

在這個(gè)HTML文件中,我們使用的編碼是GB2312,F(xiàn)orm表單中包含name和age兩個(gè)數(shù)據(jù)。首先將method設(shè)置為GET方法:

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

另外我們?cè)傩陆ㄒ粋€(gè)Web應(yīng)用程序,并在本地新建一個(gè)站點(diǎn),將端口設(shè)置為9000,添加一個(gè)頁面,名稱為WebForm1.aspx,也就是上面Form表單中的action所指向的地址http://localhost:9000/WebForm1.aspx

在點(diǎn)擊“提交”按鈕的時(shí)候,我們可以在WebForm1中獲取到網(wǎng)頁的參數(shù),具體有如下幾種方式:

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

這三種方法得到的字符串都是經(jīng)過默認(rèn)編碼轉(zhuǎn)換的,因?yàn)槲覀兪褂胿s建立項(xiàng)目時(shí)編碼默認(rèn)為UTF-8,所以這時(shí)便會(huì)出現(xiàn)亂碼。這是***種問題,稍候我們將解決這個(gè)問題。

接下來將method設(shè)置為POST方法:

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

在點(diǎn)擊“提交”按鈕的時(shí)候,我們可以在WebForm1中獲取到網(wǎng)頁的參數(shù),具體有如下幾種方式:

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

和***種問題相同,經(jīng)過默認(rèn)的UTF-8轉(zhuǎn)換,這里還會(huì)出現(xiàn)亂碼。這是第二種問題。

問題一的解決方法:

  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()]);  

另外,對(duì)于***種方法,還可以直接將URL用GB2312解碼,這里不再貼出代碼。

有了這兩種方法,不管是怎樣的亂碼,都可以高枕無憂了。

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

【編輯推薦】

  1. Web前端研發(fā)工程師編程能力飛升之路
  2. 為什么IE9是Web設(shè)計(jì)師的噩夢(mèng)
  3. 從Web技術(shù)看開源是否有利可圖?
  4. Web開發(fā)ABC:初學(xué)者必知的26個(gè)概念和技術(shù)
  5. Web開發(fā)者欣喜若狂的20款Chrome拓展
責(zé)任編輯:陳貽新 來源: 快樂的企鵝
相關(guān)推薦

2010-07-21 11:50:24

telnet亂碼

2022-09-16 08:04:25

阿里云權(quán)限網(wǎng)絡(luò)

2009-01-03 08:58:00

2009-12-11 15:37:58

Linux日志處理

2017-05-11 15:01:43

Androidweb布局

2010-06-03 10:26:29

開發(fā)MySQL中文亂碼

2011-05-05 14:51:52

一體機(jī)

2009-10-23 17:51:51

Oracle用戶密碼

2025-02-07 08:39:32

Shell部署測試

2010-09-17 14:04:14

JVM內(nèi)存設(shè)置

2011-05-12 10:54:58

一體機(jī)技巧

2020-05-11 10:59:02

PythonWord工具

2024-08-09 08:52:26

2009-11-26 16:30:52

Suse中文亂碼問題

2010-06-04 09:08:56

2010-07-27 14:25:02

linux文件編碼

2024-08-02 09:00:17

NettyWebSocketNIO

2009-11-12 10:53:57

ADO.NET連接My

2009-09-13 20:28:38

Linq插入數(shù)據(jù)

2024-08-26 08:27:18

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)