第 3 节: 3-Cookie的细节问题1
1、cookie是和浏览器相关的,不同的浏览器cookie信息是不能公用的~!!
2、浏览器开了。隐身模式的话,cookie信息也是读取不到的~!!
3、手动的将浏览器中的cookie信息删除后,也是读取不到cookie信息~!!
1、cookie是和浏览器相关的,不同的浏览器cookie信息是不能公用的~!!
2、浏览器开了。隐身模式的话,cookie信息也是读取不到的~!!
3、手动的将浏览器中的cookie信息删除后,也是读取不到cookie信息~!!
设置cookie信息:CookiesTest1 .ashx
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Web1.Day4 { /// <summary>cookies的设置 /// 服务器端做 /// </summary> public class CookiesTest1 : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; //1、调用Cookies这个类 来实例化一个Cookie对象,并命名这个Cookie对象 HttpCookie cookie = new HttpCookie("test"); //2、对这个cookie进行赋值 cookie.Value = "xiaochuanling.cn"; //3、通过Response对象的方法设置cookies报文中,并由报文传到浏览器端 context.Response.SetCookie(cookie); /* //设置值页面一个(更新cookie信息的一个写法) context.Response.SetCookie(new HttpCookie("test1","xiaochuanling.com")); * * 读取值的页面 string cookes = context.Request.Cookies["test1"].Value; */ /* //-------------cookie还可以这样设置 context.Response.Cookies["test"].Value="xiaochuanling.com"; context.Response.Cookies["test"].Expires = DateTime.Now.AddDays(3);//3天后过期 * */ } public bool IsReusable { get { return false; } } } }
读取cookie信息:CookiesTest2 .ashx
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Web1.Day4 { /// <summary> /// 服务器端读取浏览器传递过来的cookie信息 /// </summary> public class CookiesTest2 : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; //cookie就是键值对 //1、用Request的属性读取Cookie信息 HttpCookie cookie = context.Request.Cookies["test"]; //2、如果cookie信息存在,就读出,否则为null context.Response.Write(cookie==null?"没有test的cookie信息":cookie.Value); } public bool IsReusable { get { return false; } } } }
3、不同浏览器是不能公用一个cooki的,,验证如下;