asp.net Cookie值中文亂碼問題解決方法
更新時間:2013年02月17日 11:37:51 作者:
cookie里面不能寫中文,是由于cookie先天的編碼方式造成的,所以有必要存在一種中間的編碼方式:URLEncode是最好的選擇,感興趣的你可千萬不要錯過了哈,或許本文提供的知識點對你學習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時:
復制代碼 代碼如下:
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
相關(guān)文章
ASP.NET設(shè)計網(wǎng)絡硬盤之刪除文件夾實現(xiàn)代碼
對于不再需要的文件/文件夾用戶有權(quán)限做刪除處理。這一節(jié)就介紹如何實現(xiàn)這一功能2012-10-10
.NET Framework常用ORM框架iBatis.Net操作數(shù)據(jù)庫的方法
iBatis.Net 是一個輕量級的 ORM 框架,它允許開發(fā)者通過直接編寫 SQL 查詢來操作數(shù)據(jù)庫,并將查詢結(jié)果映射到對象模型中,本文將通過實際的代碼示例,詳細介紹如何在 .NET 環(huán)境中使用 iBatis.Net 進行數(shù)據(jù)庫操作,感興趣的朋友一起看看吧2024-08-08
asp.net網(wǎng)站防惡意刷新的Cookies與Session解決方法
這篇文章主要介紹了asp.net網(wǎng)站防惡意刷新的Cookies與Session解決方法,分別以實例的形式講述了采用cookie法與session法實現(xiàn)WEB頁面防止惡意刷新的技巧,需要的朋友可以參考下2014-09-09
.NET醫(yī)院公眾號系統(tǒng)線程CPU雙高問題分析
這篇文章主要介紹了.NET醫(yī)院公眾號系統(tǒng) 線程CPU雙高分析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04

