您的位置:首页 > 编程语言 > ASP

asp.net Cookie值中文乱码问题解决方法

2013-02-17 00:00 1051 查看
cookie里面不能写中文,是由于cookie先天的编码方式造成的。所以需要有一种中间编码来过渡。 URLEncode是最好的选择。

我们以asp.net为例,代码如下:
设置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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: