HttpCookie cookie = new HttpCookie("UserId")
cookie.Value = "蝈蝈"
//HttpCookie cookie = new HttpCookie("UserId", "蝈蝈")
cookie.Expires =DateTime.Now.AddMinutes(2)
HttpContext.Current.Response.AppendCookie(cookie)
//HttpContext.Current.Response.Cookies.Add(cookie)
HttpContext.Current.Response.Cookies["Pwd"].Value = "123456"
HttpContext.Current.Response.Cookies["Pwd"].Expires = DateTime.Now.AddMinutes(2)
if (HttpContext.Current.Request.Cookies[key] != null)
string value = HttpContext.Current.Request.Cookies[key];
string value = "不存在键:" + key;
if (HttpContext.Current.Request.Cookies[key] != null)
HttpCookie cookie = HttpContext.Current.Request.Cookies[key]
cookie.Value = value
HttpContext.Current.Response.Cookies.Add(cookie)
HttpContext.Current.Request.Cookies.Remove("Pwd")
删除之后HttpContext.Current.Request.Cookies[“Pwd”]还是能读取到,设置Expires属性为过去某一时间即可起到删除的效果
5、修改过期时间
if (HttpContext.Current.Request.Cookies[key] != null)
HttpCookie cookie = HttpContext.Current.Request.Cookies[key]
cookie.Expires = DateTime.Now.AddMinutes(time)
HttpContext.Current.Response.Cookies.Add(cookie)
二、多级Cookie
1、创建:
HttpCookie cookie = new HttpCookie("Guo", "MainCookieValue")
//HttpCookie cookie = new HttpCookie("Guo")
//cookie.Value = "MainCookieValue"
cookie.Expires = DateTime.Now.AddMinutes(2)
cookie.Values["Age"] = "18"
cookie.Values["Name"] = "蝈蝈"
cookie.Values.Add("Phone", "18233399999")
HttpContext.Current.Response.Cookies.Add(cookie)
//string value = HttpContext.Current.Request.Cookies["Guo"].Value
//value == MainCookieValue&Age=18&Name=蝈蝈&Phone=18233399999
if (HttpContext.Current.Request.Cookies[key] != null)
return HttpContext.Current.Request.Cookies[key][subKey] ??
"不存在键:" + key + "->" + subKey
return "不存在键:" + key
if (HttpContext.Current.Request.Cookies[key] != null)
HttpCookie cookie = HttpContext.Current.Request.Cookies[key]
cookie[subKey] = value
HttpContext.Current.Response.Cookies.Add(cookie)
HttpContext.Current.Request.Cookies["Guo"].Values.Remove("Age")
删除之后HttpContext.Current.Request.Cookies[“Guo”] [“Age”]或HttpContext.Current.Request.Cookies[“Guo”].Values[“Age”]
还是能读取到,设置Expires属性为过去某一时间即可起到删除的效果
5、修改过期时间
跟修改单级Cookie过期时间一样,只能修改一级Cookie的过期时间,不能修改二级Cookie的过期时间
三、 Session
1、创建:
HttpContext.Current.Session["UserID"] = "abc"
HttpContext.Current.Session["Pwd"] = "123"
string result = "Session[" + name + "]不存在¨²";
if (HttpContext.Current.Session[name] != null)
result = HttpContext.Current.Session[name].ToString();
string msg = string.Empty;
if (HttpContext.Current.Session[key] != null)
HttpContext.Current.Session[key] = value;
HttpContext.Current.Session[key] = value;
msg = "Session[" + key + "]==null";
return msg;
HttpContext.Current.Session.Remove(key)
Session删除确实有效果,删除之后不能读取到,但是通过设置TimeOut不能使Session马上过期,因为TimeOut的值必须是大于0的整数
5、设置过期时间
页面级的TimeOut优先级高于Web.Config中设置的timeout
Session.Timeout = 3;
<sessionState mode="InProc" timeout="1"></sessionState>
在ashx文件中使用Session需要引用using System.Web.SessionState;,然后实现IrequiresSessionState接口,
不然执行context.Session.TimeOut=1;时一直报“错误:Session=null”
cookie是键值对的形式存储少量信息,那他有什么作用?
我们知道,平时上网时都是使用无状态的HTTP协议传输出数据,这意味着客户端与服务端在数据传送完成后就会中断连接。这时我们就需要一个一直保持会话连接的机制。在session出现前,cookie就完全充当了这种角色。也就是,cookie的小量信息能帮助我们跟踪会话。一般该信息记录用户身份。
客户端请求服务器的时候,如果服务器需要记录该用户的状态,就
登录用session,举个例子,比如数据库里面已经注册了账号+密码了。
然后登录的时候。就登录进入。其中的登录过程中的流程是:先设置一个session。然后在
判断用户登录的密码+账号与数据库中的某一条账号密码对不对、对的话,就保存账号+密码数据到session中。
第一:保存账号密码到session中有什么作用?
问题解决:作用是必须登录才能使用后台的功能。登录时保存进session后(缓存中)。
然后浏览器可以根据判断缓存中有没有session。有session就一直保持登录状态。
怎么退出呢?
HttpCookie cookie = new HttpCookie("name");
cookie.Value = HttpUtility.UrlDecode("猪刚烈", Encoding.GetEncoding("UTF-8")); //HttpUtility 这个是用来编码 解码的
cookie.Expires = Da
之前的文章已经介绍了Cookie可以让服务端程序跟踪每个客户端的访问,但是每次客户端的访问都必须传回这些Cookie,如果Cookie很多,这无形地增加了客户端与服务端的数据传输量,为了解决这个问题,Session就出现了。
Session机制是一种服务器端的机制,服务器使用一种类似于散列表的结构(也可能就是使用散列表)来保存信息。
相比于保存在客户端的Cookie,Session将用户交互信息保存在了服务器端,使得同一个客户端每次和服务端交互时,不需要每次都传回所有的...
Session是基于cookie的一种绘画机制,cookie是服务器返回一小份数据到客户端,并且存放在客户端中去,session是将数据存放在服务器端
1.得到session值
HttpSession session = request.getSession();
2.常用的方法
获取会话的id:session.getId();
存值 :session.setAttribute(name,value);
取值:session.getAttribute(name);
移除值:session.removeAtt
s = requests.session()
#手动添加键为__jsl_clearance_s的cookie
requests.utils.add_dict_to_cookiejar(s.cookies,{'__jsl_clearance_s':jsl_clearance_s})
#带着刚才添加好的cookie去请求
response = s.get(url,headers=headers)
一、Cookie的介绍
cookie是用于存储Web界面的用户信息。从javascript的角度,cookie是一些字符串信息。这些信息存放在客户端的计算机中,用于客户端计算机与服务器之间传递信息。
当web服务器向浏览器发送web界面时,在连接关闭后,服务器不会记录用用户的信息。cookie的作用就是用户解决‘如何记录客户端的用户信息’:当用户访问web界面时,他的名字可以记录在cookie中,那么在用户下一次访问界面时,可以在cookie中读取用户访问记录。cookie已‘键/值’对形式存储,当浏览器从
Cookie和Session都是用于在Web应用程序中跟踪用户身份的机制,但它们的工作方式有所不同。Cookie是在客户端存储的小型文本文件,其中包含有关用户的信息,例如用户名和密码。当用户访问网站时,服务器会将Cookie发送到客户端,以便在下一次访问时使用。Session是在服务器端存储的数据结构,用于存储有关用户的信息。当用户访问网站时,服务器会创建一个唯一的Session ID,并将其存储在Cookie中。然后,服务器使用Session ID来检索与该用户相关的信息。因此,Cookie和Session都可以用于跟踪用户身份,但它们的工作方式有所不同。