asp.net Cookie值中文乱码问题解决方法
更新时间:2013年02月17日 11:37:51 作者:
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
我们以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
相关文章
.NET Core利用swagger进行API接口文档管理的方法详解
这篇文章主要给大家介绍了关于.NET Core利用swagger进行API接口文档管理的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。2018-03-03浅谈ASP.NET Core 2.0 带初始参数的中间件(译)
这篇文章主要介绍了浅谈ASP.NET Core 2.0 带初始参数的中间件(译),非常具有实用价值,需要的朋友可以参考下2017-10-10ASP .NET中执行控件(如ImageButton、LinkButton等)命令不刷新页面
在操作时,很多时候希望提交命令之后不刷新页面:1、防止页面拉动,2、防止主框架中刷新丢失当前运行的子界面。2009-03-03
最新评论