第十节 15ASP.Net中使用Cookie
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cookie1.aspx.cs" Inherits="Cookie1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="设置Cookie" onclick="Button1_Click" /> </div> <a href="Cookie_Read.aspx">点击这里去另外一个页面,读取COOKIE值</a> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; /* COOKIE * 表单和页面相关的,只有浏览器端提交了这些数据服务器端才能得到,而有时候希望在服务端任意的地方存取一些和访问者相关的信息,这时候就不方便将这些信息保存到表单中了,因为如果那样的话,必须随时浏览在所有页面表单中都保存这些信息, * Cookie是和站点相关的,并且每次向服务器请求的时候都会携带和站点相关的所有cookie,是强制性的, * cookie也是保存在浏览器端的,而且浏览器会在每次请求的时候都会把和这个站点的相关的Cookie提交到服徊器,并且将服务端返回的cookie更新回数据库,因为可以将信息保存在Cookie中,然后在服务器端读取,修改 * * 案例: 一个页面设置Cookie,一个页面读取Cookie * 设置的而面,Response.SetCookie(new HttpCookie("UserName",TextBox1.Text)) * 读取的页面: Label1.Text = Request.Cookies["UserName"].vale; * * Cookie的缺点和表单干 样,而且还不能存储过多的信息 * * 互联网优化的案例:图片服务器和主站域名不一样,降低COOKIE流量的传输 */ public partial class Cookie1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { //在客户填也能通过$.cookie取,服务端设置cookie Response.SetCookie(new HttpCookie("UserName","小向")); Response.SetCookie(new HttpCookie("Uid","1")); } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cookie_Read.aspx.cs" Inherits="Cookie_Read" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="点击读取COOKIE值" onclick="Button1_Click" /> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Cookie_Read : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string name = Request.Cookies["UserName"].Value; string id = Request.Cookies["Uid"].Value; Response.Write("UserName:"+name+", Uid:"+id); } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="变量1.aspx.cs" Inherits="变量1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> <asp:Label ID="Label1" runat="server" Text="0"></asp:Label> <br /> <br /> <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="设置Session" /> <asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="读取Session" /> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class 变量1 : System.Web.UI.Page { private int i = 11; //每次请求来了都会new一个新的实现了IHttpHandler接口的类"变量1" 的实例进行处理 //用完了就回调,所以不会保持上次的值 private static int j = 11; //所有访问都都访问同一个j的实例 protected void Page_Load(object sender, EventArgs e) { //如果取不取MySessionID if (Request.Cookies["MySessionId"] == null) { string sessionid = Guid.NewGuid().ToString(); //生成一个sessionid Response.SetCookie(new HttpCookie("MySessionId", sessionid)); } } protected void Button1_Click(object sender, EventArgs e) { Response.Write("现在i的值为:"+i.ToString()); i = i + 1; j = j + 1; Label1.Text = i.ToString(); Response.Write("现在i的值为:" + i.ToString()); Response.Write("现在j的值为:" + j.ToString()); } //设置session protected void Button2_Click(object sender, EventArgs e) { //取得保存在cookie里面的sessionid string sessionid = Request.Cookies["MySessionId"].Value; IDictionary<string, object> session = MySession.GetSession(sessionid); //session["服务端的数据"] = "333333"; //这里需要特别注意,必须在设置session值是让两行在一起,不然就是提示错误 session["服务端的数据"] = DateTime.Now.ToString(); Response.Write("session值为:" + session["另外一个值是"]); session["另外一个值是"] = "666666"; //向和这个客户端关联的服务端内存中添加键值对 //session["username"] = 20; } //读取session protected void Button3_Click(object sender, EventArgs e) { string sessionid = Request.Cookies["MySessionId"].Value; //ySession mys = new MySession(); IDictionary<string, object> session = MySession.GetSession(sessionid); //Response.Write("读取的值为:" + session["另外一个值是"]); //Convert.ToString(session["服务端的数据"]) Button3.Text = Convert.ToString(session["服务端的数据"]) + session["另外一个值是"]; } }