cookie细节问题

第 3 节: 3-Cookie的细节问题1
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的,,验证如下;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静心物语313

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值