.net 后台操作Cookies

来源:互联网 发布:vb getdc 编辑:程序博客网 时间:2024/05/17 22:06

下面是写cookie:

HttpCookie cookie = new HttpCookie("Info");//定义cookie对象以及名为Info的项

DateTime dt = DateTime.Now;//定义时间对象   

TimeSpan ts=new TimeSpan(1,0,0,0);//cookie有效作用时间,具体查msdn   

cookie.Expires = dt.Add(ts);//添加作用时间   

cookie.Values.Add("user","cxbkkk");//增加属性   

cookie.Values.Add("userid","1203");   

Response.AppendCookie(cookie);//确定写入cookie中

读取cookie   

if(Request.Cookies["Info"]!=null)   

{   

string temp=Convert.ToString(Request.Cookies["Info"].Values["user"])+" "+Convert.ToString(Request.Cookies["Info"].Values["userid"]);

}

 

如果Cookies内容为中文,应该会出现乱码,所以要HttpUtility.UrlEncode(value);

原创粉丝点击