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

前端技术 2023/09/03 .NET
cookie里面不能写中文,是由于cookie先天的编码方式造成的。所以需要有一种中间编码来过渡。 URLEncode是最好的选择。

我们以asp.net为例,代码如下:
设置Cookie时
复制代码 代码如下:

HttpCookie cookie = new HttpCookie(\"name\", System.Web.HttpContext.Current.Server.UrlEncode(\"phpstudy\"));
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

本文地址:https://www.stayed.cn/item/6886

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。