1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="delCookie.aspx.cs" Inherits="delCookie" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>Untitled Page</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:Button ID="btn_DelCookie" runat="server" Text="删除Cookie" OnClick="btn_DelCookie_Click" /><br />
13 <br />
14
15 <asp:Button ID="btn_SaveCookie" runat="server" OnClick="btn_SaveCookie_Click"
16Text="保存Cookie" />
17 <br />
18<br />
19<asp:Button ID="btn_ReadCookie" runat="server" Text="读取Cookie"
20OnClick="btn_ReadCookie_Click" /> <br />
21 <br />
22 <asp:Button ID="btn_ModifyCookie" runat="server" Text="修改Cookie" OnClick="btn_ModifyCookie_Click" /> </div>
23 </form>
24</body>
25</html>
26
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>Untitled Page</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:Button ID="btn_DelCookie" runat="server" Text="删除Cookie" OnClick="btn_DelCookie_Click" /><br />
13 <br />
14
15 <asp:Button ID="btn_SaveCookie" runat="server" OnClick="btn_SaveCookie_Click"
16Text="保存Cookie" />
17 <br />
18<br />
19<asp:Button ID="btn_ReadCookie" runat="server" Text="读取Cookie"
20OnClick="btn_ReadCookie_Click" /> <br />
21 <br />
22 <asp:Button ID="btn_ModifyCookie" runat="server" Text="修改Cookie" OnClick="btn_ModifyCookie_Click" /> </div>
23 </form>
24</body>
25</html>
26
aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class delCookie : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 服务器不能直接删除Cookie,删除Cookie的操作是浏览器进行的
/// 说是删除,其实是把它的过期时间设置为过去的时间,让Cookie过期
/// 1.从Request对象中获取Cookie。
/// 2.把Cookie的过期时间设置为过去的时间。
/// 3.把Cookie通过Response打回浏览器----(响应请求时)。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btn_DelCookie_Click(object sender, EventArgs e)
{
//Request.Cookies.AllKeys
//---get a string array containing all the keys(cookie names)
//in the cookie Collection
foreach (string key in Request.Cookies.AllKeys)
{
HttpCookie cookie = Request.Cookies[key];
cookie.Expires = DateTime.MinValue;
Response.Cookies.Add(cookie);
}
}
protected void btn_ModifyCookie_Click(object sender, EventArgs e)
{
//拿到原来的单值Cookie
HttpCookie SingleValueCookie = Request.Cookies["test1"];
//修改拿到的单值Cookie的值
SingleValueCookie.Value = "modified Cookie SingleValue";
//把Cookie通过Response打回浏览器
Response.Cookies.Add(SingleValueCookie);
}
protected void btn_SaveCookie_Click(object sender, EventArgs e)
{
//new one Cookie and Response it to Browser Side
HttpCookie SingleValueCookie = new HttpCookie("test1", "singleValueCookie");
SingleValueCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(SingleValueCookie);
//new a MultiValueCookie and Response it to Browser Side
HttpCookie MultiValueCookie = new HttpCookie("test2");
MultiValueCookie.Values.Add("key1", "value1");
MultiValueCookie.Values.Add("key2", "value2");
MultiValueCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(MultiValueCookie);
}
protected void btn_ReadCookie_Click(object sender, EventArgs e)
{
HttpCookie SingleValueCookie = Request.Cookies["test1"];
if (SingleValueCookie != null)
{
Response.Write(String.Format("key:{0};Value:{1};Expires:{2}<br>", "test1", SingleValueCookie.Value, SingleValueCookie.Expires));
Response.Write("--------------"+"<br>");
}
HttpCookie MultiValueCookie = Request.Cookies["test2"];
if (MultiValueCookie != null)
{
Response.Write(String.Format("key:{0};value:{1}<br>", "test2", MultiValueCookie.Value));
Response.Write("--------------" + "<br>");
foreach (string subkey in MultiValueCookie.Values.AllKeys)
{
Response.Write(String.Format("key:{0};values:{1};Expirs:{2}", subkey, MultiValueCookie.Values[subkey], MultiValueCookie.Expires));
}
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class delCookie : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 服务器不能直接删除Cookie,删除Cookie的操作是浏览器进行的
/// 说是删除,其实是把它的过期时间设置为过去的时间,让Cookie过期
/// 1.从Request对象中获取Cookie。
/// 2.把Cookie的过期时间设置为过去的时间。
/// 3.把Cookie通过Response打回浏览器----(响应请求时)。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btn_DelCookie_Click(object sender, EventArgs e)
{
//Request.Cookies.AllKeys
//---get a string array containing all the keys(cookie names)
//in the cookie Collection
foreach (string key in Request.Cookies.AllKeys)
{
HttpCookie cookie = Request.Cookies[key];
cookie.Expires = DateTime.MinValue;
Response.Cookies.Add(cookie);
}
}
protected void btn_ModifyCookie_Click(object sender, EventArgs e)
{
//拿到原来的单值Cookie
HttpCookie SingleValueCookie = Request.Cookies["test1"];
//修改拿到的单值Cookie的值
SingleValueCookie.Value = "modified Cookie SingleValue";
//把Cookie通过Response打回浏览器
Response.Cookies.Add(SingleValueCookie);
}
protected void btn_SaveCookie_Click(object sender, EventArgs e)
{
//new one Cookie and Response it to Browser Side
HttpCookie SingleValueCookie = new HttpCookie("test1", "singleValueCookie");
SingleValueCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(SingleValueCookie);
//new a MultiValueCookie and Response it to Browser Side
HttpCookie MultiValueCookie = new HttpCookie("test2");
MultiValueCookie.Values.Add("key1", "value1");
MultiValueCookie.Values.Add("key2", "value2");
MultiValueCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(MultiValueCookie);
}
protected void btn_ReadCookie_Click(object sender, EventArgs e)
{
HttpCookie SingleValueCookie = Request.Cookies["test1"];
if (SingleValueCookie != null)
{
Response.Write(String.Format("key:{0};Value:{1};Expires:{2}<br>", "test1", SingleValueCookie.Value, SingleValueCookie.Expires));
Response.Write("--------------"+"<br>");
}
HttpCookie MultiValueCookie = Request.Cookies["test2"];
if (MultiValueCookie != null)
{
Response.Write(String.Format("key:{0};value:{1}<br>", "test2", MultiValueCookie.Value));
Response.Write("--------------" + "<br>");
foreach (string subkey in MultiValueCookie.Values.AllKeys)
{
Response.Write(String.Format("key:{0};values:{1};Expirs:{2}", subkey, MultiValueCookie.Values[subkey], MultiValueCookie.Expires));
}
}
}
}