asp.net Cookie值中文亂碼問題解決方法
更新時間:2013年02月17日 11:37:51 作者:
cookie里面不能寫中文,是由于cookie先天的編碼方式造成的,所以有必要存在一種中間的編碼方式:URLEncode是最好的選擇,感興趣的你可千萬不要錯過了哈,或許本文提供的知識點對你學(xué)習(xí)cookie有所幫助
cookie里面不能寫中文,是由于cookie先天的編碼方式造成的。所以需要有一種中間編碼來過渡。 URLEncode是最好的選擇。
我們以asp.net為例,代碼如下:
設(shè)置Cookie時:
HttpCookie cookie = new HttpCookie("name", System.Web.HttpContext.Current.Server.UrlEncode("腳本之家"));
Response.Cookies.Add(cookie);讀取Cookie時:
if (Request.Cookies["name"] != null)
{
Response.Write(System.Web.HttpContext.Current.Server.UrlDecode(Request.Cookies["name"].Value));
}
注意:編碼和解碼要一致
System.Web.HttpContext.Current.Server.UrlDecode 和 System.Web.HttpContext.Current.Server.UrlEncode
System.Web.HttpUtility.UrlDecode 和 System.Web.HttpUtility.UrlEncode
我們以asp.net為例,代碼如下:
設(shè)置Cookie時:
復(fù)制代碼 代碼如下:
HttpCookie cookie = new HttpCookie("name", System.Web.HttpContext.Current.Server.UrlEncode("腳本之家"));
Response.Cookies.Add(cookie);讀取Cookie時:
if (Request.Cookies["name"] != null)
{
Response.Write(System.Web.HttpContext.Current.Server.UrlDecode(Request.Cookies["name"].Value));
}
注意:編碼和解碼要一致
復(fù)制代碼 代碼如下:
System.Web.HttpContext.Current.Server.UrlDecode 和 System.Web.HttpContext.Current.Server.UrlEncode
System.Web.HttpUtility.UrlDecode 和 System.Web.HttpUtility.UrlEncode
相關(guān)文章
asp.net下數(shù)據(jù)庫操作優(yōu)化一例
數(shù)據(jù)庫升級,需要對幾個表進(jìn)行一些數(shù)據(jù)轉(zhuǎn)換,具體是這樣:針對每一個 Item,從 orders 表里查出 Shop_Id,并把此 Id 賦值給 items 和 skus 中的 Shop_Id。2010-11-11ASP.net(C#)從其他網(wǎng)站抓取內(nèi)容并截取有用信息的實現(xiàn)代碼
ASP.net(C#)從其他網(wǎng)站抓取內(nèi)容并截取有用信息的實現(xiàn)代碼,需要的朋友可以參考下。2011-09-09ASP.NET Core 集成 React SPA應(yīng)用的步驟
這篇文章主要介紹了ASP.NET Core 集成 React SPA應(yīng)用的步驟,幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下2021-04-04Asp.net 2.0 無刷新圖片上傳 顯示縮略圖 具體實現(xiàn)
簡單三步實現(xiàn)圖片無刷新上傳:注意是上傳,至于上傳時的驗證,比如圖片的尺寸,大小,格式。自行解決。如果我搞定了,也會貼上來的。2013-06-06