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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-18 我要评论
想了解asp.net Cookie值中文乱码问题解决办法的相关内容吗,在本文为您仔细讲解asp.net cookie乱码的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们