服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - asp.net Cookie值中文乱码问题解决方法

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

2019-10-23 13:36asp.net教程网 ASP.NET教程

cookie里面不能写中文,是由于cookie先天的编码方式造成的,所以有必要存在一种中间的编码方式:URLEncode是最好的选择,感兴趣的你可千万不要错过了哈,或许本文提供的知识点对你学习cookie有所帮助

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 

延伸 · 阅读

精彩推荐