013-Cookie状态保持

常用的状态(信息)保持方式(重点)

ViewState:
ASP.NET 的 .aspx页面特有,页面级的;
就是在页面上的一个隐藏域中保存客户端单独使用的数据的一种方式;
服务器端控件的值都自动保存在ViewState中;
Cookie:
HTTP协议下的一种方式,通过该方式,服务器或脚本能够在客户机上维护状态信息;
就是在客户端保存客户端单独使用的数据的一种方式;
就像你的病历本一样,医院直接给你带回家;
Session:
现在指的是进程内Session。
在服务器端保存客户端单独使用的数据的一种方式;
就像银行账户,钱都存在银行里,你就拿一张银行卡(SessionId)回家;
Application:
在服务器端保存共享数据的一种方式;
就像银行的单人公共卫生间,谁进去都行,但一次去一个,进去了就锁上门,出来再把锁打开;

-》Cookie
-》类型HttpCookie,主要属性包括:Name(键)、Value(值)、Expires(过期时间)
-》读:Request.Cookies["键"],返回HttpCookie对象,通过Value属性获取值
-》写:Response.Cookies.Add(对象)
-》说明:默认有效时间为进程内有效,浏览器关闭则失效
-》传输:通过http协议的请求头、响应头,在浏览器与服务器间传输
-》示例1:记录上次访问时间
查看报文中的cookie信息

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CookieTest.aspx.cs" Inherits="t4_State.CookieTest" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 8     <title></title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12         <div>
13 
14             <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
15             <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
16 
17         </div>
18     </form>
19 </body>
20 </html>
 1     public partial class CookieTest : System.Web.UI.Page
 2     {
 3         protected void Page_Load(object sender, EventArgs e)
 4         {
 5             if (!IsPostBack)
 6             {
 7                 //写cookie
 8                 HttpCookie cookie = new HttpCookie("name", Server.UrlEncode("杨过"));
 9                 //一天后过期
10                 //cookie.Expires = DateTime.Now.AddDays(1);
11                 //一秒前过期
12                 cookie.Expires = DateTime.Now.AddMinutes(-1);
13                 Response.Cookies.Add(cookie);
14             }
15         }
16 
17         protected void Button1_Click(object sender, EventArgs e)
18         {
19             //读取cookie
20             HttpCookie cookie = Request.Cookies["name"];
21             if (cookie != null)
22             {
23                 Label1.Text = Server.UrlDecode(cookie.Value);
24             }
25         }
26     }

-》示例2:跨页面共享信息

CookieWrite.aspx

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CookieWrite.aspx.cs" Inherits="t4_State.CookieWrite" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 8     <title></title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12         <div>
13             <a href="CookieRead.aspx">读取Cookie</a>
14         </div>
15     </form>
16 </body>
17 </html>
1     public partial class CookieWrite : System.Web.UI.Page
2     {
3         protected void Page_Load(object sender, EventArgs e)
4         {
5             HttpCookie cookie=new HttpCookie("name","abc");
6             Response.Cookies.Add(cookie);
7         }
8     }

CookieRead.aspx

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CookieRead.aspx.cs" Inherits="t4_State.CookieRead" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 8     <title></title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12         <div>
13         </div>
14     </form>
15 </body>
16 </html>
 1     public partial class CookieRead : System.Web.UI.Page
 2     {
 3         protected void Page_Load(object sender, EventArgs e)
 4         {
 5             HttpCookie cookie = Request.Cookies["name"];
 6             if (cookie != null)
 7             {
 8                 Response.Write(cookie.Value);
 9             }
10         }
11     }
原文地址:https://www.cnblogs.com/ninghongkun/p/6288286.html