本文内容
- 单值 COOKIE
- 多值 COOKIE
- 限制 COOKIE
- 遍历 COOKIE
- 更改 COOKIE
- 确定浏览器是否支持 COOKIE
单值 COOKIE
SingleCOOKIE.aspx 页面
<%&#64; Page Language&#61;"C#" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns&#61;"http://www.w3.org/1999/xhtml"> <head runat&#61;"server"> <title>title> <script runat&#61;"server"> 1: 2: protected void BtnCOOKIE_Click(object sender, EventArgs e) 3: { 4: HttpCOOKIE COOKIEName &#61; new HttpCOOKIE("Name"); 5: HttpCOOKIE COOKIELastVisit &#61; new HttpCOOKIE("lastVisit"); 6: 7: COOKIEName.Value &#61; TextBox1.Text; 8: COOKIEName.Expires &#61; DateTime.Now.AddDays(1); 9: 10: COOKIELastVisit.Value &#61; DateTime.Now.ToString(); 11: COOKIELastVisit.Expires &#61; DateTime.Now.AddDays(1); 12: 13: Response.AppendCOOKIE(COOKIEName); 14: Response.AppendCOOKIE(COOKIELastVisit); 15: //Response.COOKIEs.Add(COOKIEName); 16: //Response.COOKIEs.Add(COOKIELastVisit); 17: 18: Response.Redirect("SingleCOOKIERec.aspx"); 19: //Server.Transfer("SingleCOOKIERec.aspx"); 20: } 21: //protected void BtnCOOKIE_Click(object sender, EventArgs e) 22: //{ 23: // Response.COOKIEs["Name"].Value &#61; TextBox1.Text; 24: // Response.COOKIEs["Name"].Expires &#61; DateTime.Now.AddDays(1); 25: 26: // HttpCOOKIE aCOOKIE &#61; new HttpCOOKIE("lastVisit"); 27: // aCOOKIE.Value &#61; DateTime.Now.ToString(); 28: // aCOOKIE.Expires &#61; DateTime.Now.AddDays(1); 29: 30: // Response.COOKIEs.Add(aCOOKIE); 31: 32: // Response.Redirect("SingleCOOKIERec.aspx"); 33: //} 34: script> head> <body> <form id&#61;"form1" runat&#61;"server"> <div> <asp:TextBox ID&#61;"TextBox1" runat&#61;"server" ForeColor&#61;"Red" Width&#61;"250px">Nameasp:TextBox> <asp:Button ID&#61;"BtnCOOKIE" runat&#61;"server" OnClick&#61;"BtnCOOKIE_Click" Text&#61;"BtnCOOKIE" /> div> form> body> html>
SingleCOOKIERec.aspx 页面
<%&#64; Page Language&#61;"C#" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns&#61;"http://www.w3.org/1999/xhtml"> <head runat&#61;"server"> <title>title> <script runat&#61;"server"> 1: 2: protected void Page_Load(object sender, EventArgs e) 3: { 4: #region 读取COOKIE 5: string value &#61; string.Empty; 6: if (Request.COOKIEs["Name"] !&#61; null) 7: { 8: value &#61; Server.HtmlEncode(Request.COOKIEs["Name"].ToString()); 9: Response.Write(value &#43; " 10: } 11: if (Request.COOKIEs["lastVisit"] !&#61; null) 12: { 13: HttpCOOKIE aCOOKIE &#61; Request.COOKIEs["lastVisit"]; 14: value &#61; Server.HtmlEncode(aCOOKIE.Value); 15: Response.Write(value &#43; " 16: } 17: 18: #endregion 19: #region 删除COOKIE 20: HttpCOOKIE dCOOKIE; 21: string COOKIEName; 22: int limit &#61; Request.COOKIEs.Count; 23: for (int i &#61; 0; i 24: { 25: COOKIEName &#61; Request.COOKIEs[i].Name; 26: dCOOKIE &#61; new HttpCOOKIE(COOKIEName); 27: dCOOKIE.Expires &#61; DateTime.Now.AddDays(-1); 28: Response.Write("COOKIEs 的内容&#xff1a;" &#43; COOKIEName &#43; ""); 29: Response.COOKIEs.Add(dCOOKIE); 30: } 31: #endregion 32: } 33: script> head> <body> <form id&#61;"form1" runat&#61;"server"> <div> div> form> body> html>
");
");
说明
(1) 本例演示向COOKIEs集合添加两个COOKIE&#xff0c;读取完后删除&#xff1b;
(2) 可以用两种方式向COOKIEs集合添加COOKIE&#xff1b;
(3) 一种方式是创建一个HttpCOOKIE类的对象&#xff0c;设置相应属性&#xff0c;再通过Add方法将其添加到COOKIEs集合&#xff0c;既可以用Response.AppendCOOKIE()方法&#xff0c;也可以用Response.COOKIEs.Add()方法&#xff1b;
(4) 另一种是直接设置COOKIEs&#xff1b;
(5) 读取时&#xff0c;要注意使用Server.HtmlEncode()。
多值 COOKIE
MultiCOOKIE.aspx 页面
<%&#64; Page Language&#61;"C#" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns&#61;"http://www.w3.org/1999/xhtml"> <head runat&#61;"server"> <title>title> <script runat&#61;"server"> 1: 2: protected void BtnCOOKIE_Click(object sender, EventArgs e) 3: { 4: Response.COOKIEs["userInfo"]["userName"] &#61; "anonymous "; 5: Response.COOKIEs["userInfo"]["lastVisit"] &#61; DateTime.Now.ToString(); 6: Response.COOKIEs["userInfo"].Expires &#61; DateTime.Now.AddDays(1); 7: 8: //HttpCOOKIE aCOOKIE &#61; new HttpCOOKIE("userInfo"); 9: //aCOOKIE.Values["userName"] &#61; "anonymous"; 10: //aCOOKIE.Values["lastVisit"] &#61; DateTime.Now.ToString(); 11: //aCOOKIE.Expires &#61; DateTime.Now.AddDays(1); 12: //Response.COOKIEs.Add(aCOOKIE); 13: Response.Redirect("MultiCOOKIERec.aspx"); 14: } 15: script> head> <body> <form id&#61;"form1" runat&#61;"server"> <asp:Button ID&#61;"BtnCOOKIE" runat&#61;"server" OnClick&#61;"BtnCOOKIE_Click" Text&#61;"BtnCOOKIE" /> form> body> html>
MultiCOOKIERec.aspx 页面
<%&#64; Page Language&#61;"C#" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns&#61;"http://www.w3.org/1999/xhtml"> <head runat&#61;"server"> <title>title> <script runat&#61;"server"> 1: 2: protected void Page_Load(object sender, EventArgs e) 3: { 4: if (Request.COOKIEs["userInfo"] !&#61; null) 5: { 6: Response.Write(Server.HtmlEncode(Request.COOKIEs["userInfo"]["userName"]) &#43; ""); 7: Response.Write(Server.HtmlEncode(Request.COOKIEs["userInfo"]["lastVisit"]) &#43; ""); 8: //DateTime.Parse(Request.COOKIEs["userInfo"]["lastVisit"]); 9: } 10: //if (Request.COOKIEs["userInfo"] !&#61; null) 11: //{ 12: // System.Collections.Specialized.NameValueCollection UserInfoCOOKIECollection; 13: // UserInfoCOOKIECollection &#61; Request.COOKIEs["userInfo"].Values; 14: // Response.Write(Server.HtmlEncode(UserInfoCOOKIECollection["userName"]) &#43; ""); 15: // Response.Write(Server.HtmlEncode(UserInfoCOOKIECollection["lastVisit"]) &#43; ""); 16: //} 17: HttpCOOKIE dCOOKIE; 18: string COOKIEName; 19: int limit &#61; Request.COOKIEs.Count; 20: for (int i &#61; 0; i 21: { 22: COOKIEName &#61; Request.COOKIEs[i].Name; 23: dCOOKIE &#61; new HttpCOOKIE(COOKIEName); 24: dCOOKIE.Expires &#61; DateTime.Now.AddDays(-1); 25: Response.Write("COOKIEs 的内容&#xff1a;" &#43; COOKIEName &#43; ""); 26: Response.COOKIEs.Add(dCOOKIE); 27: } 28: } 29: script> head> <body> <form id&#61;"form1" runat&#61;"server"> <div> div> form> body> html>
说明
(1) 本例演示多值COOKIE&#xff0c;读取后删除&#xff1b;
(2) 可以用两种方式读取。
限制 COOKIE
限制到某个文件夹或应用程序
HttpCOOKIE appCOOKIE &#61; new HttpCOOKIE("AppCOOKIE");
appCOOKIE.Value &#61; "written " &#43; DateTime.Now.ToString();
appCOOKIE.Expires &#61; DateTime.Now.AddDays(1);
appCOOKIE.Path &#61; "~/COOKIEApplication";
Response.COOKIEs.Add(appCOOKIE);
Response.Redirect("COOKIEApplication/Default.aspx");
说明
(1) 路径可以是站点根目录下的物理路径&#xff0c;也可以是虚拟根目录&#xff1b;
(2) 所产生的效果是 COOKIE 只能用于COOKIEApplication文件夹或虚拟根目录中的页面。
例如&#xff0c;如果您的站点名称为 www.contoso.com&#xff0c;则在前面示例中创建的 COOKIE 将只能用于路径为 http://www.contoso.com/Application1/ 的页面以及该文件夹下的所有页面。
但是&#xff0c;COOKIE 将不能用于其他应用程序中的页面&#xff0c;如 http://www.contoso.com/Application2/ 或 http://www.contoso.com/ 中的页面。
限制 COOKIE 的域范围
Response.COOKIEs["domain"].Value &#61; DateTime.Now.ToString();
Response.COOKIEs["domain"].Expires &#61; DateTime.Now.AddDays(1);
Response.COOKIEs["domain"].Domain &#61; "contoso.com";
说明
(1) 默认情况下&#xff0c;COOKIE 与特定域关联。
例如&#xff0c;如果您的站点是 www.contoso.com&#xff0c;那么当用户向该站点请求任何页时&#xff0c;您编写的 COOKIE 就会被发送到服务器。
如果站点具有子域&#xff08;例如&#xff0c;contoso.com、sales.contoso.com 和 support.contoso.com&#xff09;&#xff0c;则可以将 COOKIE 与特定的子域关联。
当以此方式设置域时&#xff0c;COOKIE将仅可用于指定的子域中的页面。还可以使用 Domain 属性创建可在多个子域间共享的 COOKIE。
(2) 本例的COOKIE将可用于主域&#xff0c;也可用于 sales.contoso.com 和 support.contoso.com 域。
遍历 COOKIE
StringBuilder output &#61; new StringBuilder();
HttpCOOKIE aCOOKIE;
for (int i &#61; 0; i { aCOOKIE &#61; Request.COOKIEs[i]; output.Append("COOKIE name &#61; " &#43; Server.HtmlEncode(aCOOKIE.Name) &#43; " output.Append("COOKIE value &#61; " &#43; Server.HtmlEncode(aCOOKIE.Value) &#43; " } Response.Write(output.ToString());
");
");
但是&#xff0c;上面这个示例有一个限制&#xff1a;
(1) 如果 COOKIE 有子键&#xff0c;则会以一个名称/值字符串来显示子键。
(2) 可以读取 COOKIE 的 HasKeys 属性&#xff0c;以确定 COOKIE 是否有子键。如果有&#xff0c;则可以读取子键集合&#xff1b;
(3) 通过索引值直接从 Values 集合中读取子键值。相应的子键名称可在 Values 集合的 AllKeys 成员中获得&#xff1b;
(4) 还可以使用 Values 集合的 Keys 成员。但是&#xff0c;首次访问 AllKeys 属性时&#xff0c;该属性会被缓存&#xff1b;
(5) 相比之下&#xff0c;每次访问 Keys 属性时&#xff0c;该属性都生成一个数组。因此在同一页请求的上下文内&#xff0c;在随后访问时&#xff0c;AllKeys 属性要快得多。
for (int i &#61; 0; i { aCOOKIE &#61; Request.COOKIEs[i]; output.Append("Name &#61; " &#43; aCOOKIE.Name &#43; " if (aCOOKIE.HasKeys) { for (int j &#61; 0; j { subkeyName &#61; Server.HtmlEncode(aCOOKIE.Values.AllKeys[j]); subkeyValue &#61; Server.HtmlEncode(aCOOKIE.Values[j]); output.Append("Subkey name &#61; " &#43; subkeyName &#43; " output.Append("Subkey value &#61; " &#43; subkeyValue &#43; " } } else { output.Append("Value &#61; " &#43; Server.HtmlEncode(aCOOKIE.Value) &#43; " } } Response.Write(output.ToString());
");
");
");
");
或者&#xff0c;可将子键作为 NameValueCollection 对象提取&#xff1a;
System.Text.StringBuilder output &#61; new System.Text.StringBuilder();
HttpCOOKIE aCOOKIE;
string subkeyName;
string subkeyValue;
for (int i &#61; 0; i { aCOOKIE &#61; Request.COOKIEs[i]; output.Append("Name &#61; " &#43; aCOOKIE.Name &#43; " if (aCOOKIE.HasKeys) { System.Collections.Specialized.NameValueCollection COOKIEValues &#61; aCOOKIE.Values; string[] COOKIEValueNames &#61; COOKIEValues.AllKeys; for (int j &#61; 0; j { subkeyName &#61; Server.HtmlEncode(COOKIEValueNames[j]); subkeyValue &#61; Server.HtmlEncode(COOKIEValues[j]); output.Append("Subkey name &#61; " &#43; subkeyName &#43; " output.Append("Subkey value &#61; " &#43; subkeyValue &#43; " } } else { output.Append("Value &#61; " &#43; Server.HtmlEncode(aCOOKIE.Value) &#43; " } } Response.Write(output.ToString());
");
");
");
");
更改 COOKIE
int counter;
if (Request.COOKIEs["counter"] &#61;&#61; null)
counter &#61; 0;
else
{
counter &#61; int.Parse(Request.COOKIEs["counter"].Value);
}
counter&#43;&#43;;
Response.COOKIEs["counter"].Value &#61; counter.ToString();
Response.COOKIEs["counter"].Expires &#61; DateTime.Now.AddDays(1);
说明
(1) 本代码演示如何更改存储用户对站点的访问次数的 COOKIE 的值&#xff1b;
(1) 不能直接修改 COOKIE。更改 COOKIE 的过程涉及创建一个有新值的新 COOKIE&#xff0c;然后将其发送到浏览器来覆盖客户端上的旧版本 COOKIE。
确定浏览器是否支持 COOKIE
SupportCOOKIE.aspx 页面
<%&#64; Page Language&#61;"C#" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns&#61;"http://www.w3.org/1999/xhtml"> <head runat&#61;"server"> <title>title> <script runat&#61;"server"> 1: 2: protected void Page_Load(object sender, EventArgs e) 3: { 4: if (!Page.IsPostBack) 5: { 6: if (Request.QueryString["AcceptsCOOKIEs"] &#61;&#61; null) 7: { 8: Response.COOKIEs["TestCOOKIE"].Value &#61; "ok"; 9: Response.COOKIEs["TestCOOKIE"].Expires &#61; DateTime.Now.AddMinutes(1); 10: Response.Redirect("SupportCOOKIERec.aspx?redirect&#61;" &#43; Server.UrlEncode(Request.Url.ToString())); 11: } 12: else 13: { 14: Label1.Text &#61; "Accept COOKIEs &#61; " &#43; Server.UrlEncode(Request.QueryString["AcceptsCOOKIEs"]); 15: } 16: } 17: } 18: script> head> <body> <form id&#61;"form1" runat&#61;"server"> <asp:Label ID&#61;"Label1" runat&#61;"server" Text&#61;"Label">asp:Label> form> body> html>
SupportCOOKIERec.aspx 页面
<%&#64; Page Language&#61;"C#" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns&#61;"http://www.w3.org/1999/xhtml"> <head runat&#61;"server"> <title>title> <script runat&#61;"server"> 1: 2: protected void Page_Load(object sender, EventArgs e) 3: { 4: string redirect &#61; Request.QueryString["redirect"]; 5: string acceptsCOOKIEs; 6: if (Request.COOKIEs["TestCOOKIE"] &#61;&#61; null) 7: acceptsCOOKIEs &#61; "no"; 8: else 9: { 10: acceptsCOOKIEs &#61; "yes"; 11: Response.COOKIEs["TestCOOKIE"].Expires &#61; DateTime.Now.AddDays(-1); 12: } 13: Response.Redirect(redirect &#43; "?AcceptsCOOKIEs&#61;" &#43; acceptsCOOKIEs, true); 14: } 15: script> head> <body> <form id&#61;"form1" runat&#61;"server"> <div> div> form> body> html>
说明
(1) 用户可将其浏览器设置为拒绝接受 COOKIE。在不能写入 COOKIE 时不会引发任何错误。同样&#xff0c;浏览器也不向服务器发送有关其当前 COOKIE 设置的任何信息&#xff1b;
(2) 确定 COOKIE 是否被接受的一种方法是尝试编写一个 COOKIE&#xff0c;然后再尝试读取该 COOKIE。如果无法读取您编写的 COOKIE&#xff0c;则可以假定浏览器不接受 COOKIE。
下载 Demo