COOKIE是一段文本信息,在客户端存储 COOKIE 是 ASP.NET 的会话状态将请求与会话关联的方法之一。COOKIE 也可以直接用于在请求之间保持数据,但数据随后将存储在客户端并随每个请求一起发送到服务器。浏览器对 COOKIE 的大小有限制,因此,只有不超过 4096 字节才能保证被接受。
编写COOKIE
代码
1 //方式1:
2 Response.COOKIEs["username"].value="mike";
3 Response.COOKIEs["username"].Expires=DateTime.MaxValue;
4
5 //方式2:
6 HttpCOOKIE aCOOKIE = new HttpCOOKIE("last");
7 aCOOKIE.Value="a";
8 aCOOKIE..Expires=DateTime.MaxValue;
9 Response.COOKIEs.Add(aCOOKIE);
10
11 //多值COOKIE的写法
12
13 //方式1:
14 Response.COOKIEs["userinfo1"]["name"].value="mike";
15 Response.COOKIEs["userinfo1"]["last"].value="a";
16 Response.COOKIEs["userinfo1"].Expires=DateTime.MaxValue;
17
18 //方式2:
19 HttpCOOKIE COOKIE = new HttpCOOKIE("userinfo1");
20 COOKIE.Values["name"]="mike";
21 COOKIE.Values["last"]="a";
22 COOKIE.Expires=DateTime.MaxValue;
23 //COOKIE.Expires = System.DateTime.Now.AddDays(1);//设置过期时间 1天
24 Response.COOKIEs.Add(COOKIE);
25
26
2 Response.COOKIEs["username"].value="mike";
3 Response.COOKIEs["username"].Expires=DateTime.MaxValue;
4
5 //方式2:
6 HttpCOOKIE aCOOKIE = new HttpCOOKIE("last");
7 aCOOKIE.Value="a";
8 aCOOKIE..Expires=DateTime.MaxValue;
9 Response.COOKIEs.Add(aCOOKIE);
10
11 //多值COOKIE的写法
12
13 //方式1:
14 Response.COOKIEs["userinfo1"]["name"].value="mike";
15 Response.COOKIEs["userinfo1"]["last"].value="a";
16 Response.COOKIEs["userinfo1"].Expires=DateTime.MaxValue;
17
18 //方式2:
19 HttpCOOKIE COOKIE = new HttpCOOKIE("userinfo1");
20 COOKIE.Values["name"]="mike";
21 COOKIE.Values["last"]="a";
22 COOKIE.Expires=DateTime.MaxValue;
23 //COOKIE.Expires = System.DateTime.Now.AddDays(1);//设置过期时间 1天
24 Response.COOKIEs.Add(COOKIE);
25
26
运行此代码时,可看到一个名为“ASP.NET_SessionId”的COOKIE,ASP.NET用这个 COOKIE 来保存您的会话的唯一标识符。
修改 COOKIE
修改的方法与创建方法相同
代码
修改COOKIE
1 Response.COOKIEs["Info"]["user"] = "2";
2 Response.COOKIEs["Info"].Expires = DateTime.Now.AddDays(1); 删除COOKIE下的属性
1 HttpCOOKIE aCOOKIE=Request.COOKIEs["Info"];
2 aCOOKIE.Values.Remove("userid");
3 aCOOKIE.Expires = DateTime.Now.AddDays(1);
4 Response.COOKIEs.Add(aCOOKIE); 删除所有COOKIE,就是设置过期时间为现在就行了
1 int limit=Request.COOKIEs.Count - 1;
2 for(int i&#61;0;i<limit;i&#43;&#43;)
3 {
4 aCOOKIE &#61; Request.COOKIEs(i)
5 aCOOKIE.Expires &#61; DateTime.Now.AddDays(-1)
6 Response.COOKIEs.Add(aCOOKIE)
7 }
1 Response.COOKIEs["Info"]["user"] &#61; "2";
2 Response.COOKIEs["Info"].Expires &#61; DateTime.Now.AddDays(1); 删除COOKIE下的属性
1 HttpCOOKIE aCOOKIE&#61;Request.COOKIEs["Info"];
2 aCOOKIE.Values.Remove("userid");
3 aCOOKIE.Expires &#61; DateTime.Now.AddDays(1);
4 Response.COOKIEs.Add(aCOOKIE); 删除所有COOKIE&#xff0c;就是设置过期时间为现在就行了
1 int limit&#61;Request.COOKIEs.Count - 1;
2 for(int i&#61;0;i<limit;i&#43;&#43;)
3 {
4 aCOOKIE &#61; Request.COOKIEs(i)
5 aCOOKIE.Expires &#61; DateTime.Now.AddDays(-1)
6 Response.COOKIEs.Add(aCOOKIE)
7 }
删除 COOKIE
将其有效期设置为过去的某个日期。当浏览器检查 COOKIE 的有效期时&#xff0c;就会删除这个已过期的 COOKIE。
HttpCOOKIE COOKIE &#61; new HttpCOOKIE("userinfo1");
COOKIE.Expires&#61;DateTime.Now.AddDays(-30);
Response.COOKIEs.Add(COOKIE);
COOKIE.Expires&#61;DateTime.Now.AddDays(-30);
Response.COOKIEs.Add(COOKIE);
读取COOKIE
Internet Explorer 将站点的 COOKIE 保存在文件名格式为
注意:在获取COOKIE的值之前&#xff0c;应该确保该 COOKIE 确实存在。否则&#xff0c;您将得到一个异常
代码
If (Request.COOKIEs["userName"]!&#61;null)
{
string str &#61; Request.COOKIEs("userName").Value;
}
//多值COOKIE的读取
If ( Request.COOKIEs["userInfo1"]!&#61;null )
{
string name&#61;Request.COOKIEs["userInfo1"]["name"];
string last&#61;Request.COOKIEs["userInfo1"]["last"];
}
//读取 COOKIE 集合
for(int i &#61; 0 ;i<Request.COOKIEs.Count ;i&#43;&#43;)
{
HttpCOOKIE COOKIEs &#61; Request.COOKIEs;
Response.Write("name&#61;"&#43;COOKIEs.Mame&#43;"
");
if (COOKIEs.HasKeys )//是否有子键
{
System.Collections.Specialized.NameValueCollection NameColl
&#61; aCOOKIE.Values ;
for(int j&#61;0;j<NameColl.Count;j&#43;&#43;)
{
Response.Write("子键名&#61;"&#43; NameColl.AllKey[j] &#43;"
");
Response.Write("子键值&#61;"&#43; NameColl[j] &#43;"
");
}
}
else
{
Response.Write("value&#61;"&#43;COOKIEs.Value&#43;"
");
}
}
{
string str &#61; Request.COOKIEs("userName").Value;
}
//多值COOKIE的读取
If ( Request.COOKIEs["userInfo1"]!&#61;null )
{
string name&#61;Request.COOKIEs["userInfo1"]["name"];
string last&#61;Request.COOKIEs["userInfo1"]["last"];
}
//读取 COOKIE 集合
for(int i &#61; 0 ;i<Request.COOKIEs.Count ;i&#43;&#43;)
{
HttpCOOKIE COOKIEs &#61; Request.COOKIEs;
Response.Write("name&#61;"&#43;COOKIEs.Mame&#43;"
");
if (COOKIEs.HasKeys )//是否有子键
{
System.Collections.Specialized.NameValueCollection NameColl
&#61; aCOOKIE.Values ;
for(int j&#61;0;j<NameColl.Count;j&#43;&#43;)
{
Response.Write("子键名&#61;"&#43; NameColl.AllKey[j] &#43;"
");
Response.Write("子键值&#61;"&#43; NameColl[j] &#43;"
");
}
}
else
{
Response.Write("value&#61;"&#43;COOKIEs.Value&#43;"
");
}
}
代码
//----------写入COOKIE
HttpCOOKIE COOKIE &#61; new HttpCOOKIE("userinfo1");
COOKIE.Expires &#61; System.DateTime.Now.AddMinutes(2);//设置过期时间
for (int i &#61; 0; i < 20; i&#43;&#43;)
{
COOKIE.Values["Val" &#43;i.ToString()] &#61; i.ToString();
Response.COOKIEs.Add(COOKIE);
}
//读取 COOKIE 集合
for (int i &#61; 0; i < Request.COOKIEs.Count; i&#43;&#43;)
{
if (Request.COOKIEs.AllKeys[i] &#61;&#61; "userinfo1")
{
HttpCOOKIE COOKIEs &#61; Request.COOKIEs["userInfo1"];
Response.Write("name&#61;" &#43; COOKIEs.Name &#43; "
");
//Response.Write("name&#61;" &#43; COOKIEs.Value &#43; "
");
if (COOKIEs.HasKeys)//是否有子键
{
System.Collections.Specialized.NameValueCollection NameColl &#61; COOKIEs.Values;
for (int j &#61; 0; j < NameColl.Count; j&#43;&#43;)
{
Response.Write("子键名&#61;" &#43; NameColl.AllKeys[j] &#43; "
");
Response.Write("子键值&#61;" &#43; NameColl[j] &#43; "
");
}
}
else
{
Response.Write("value&#61;" &#43; COOKIEs.Value &#43; "
");
}
}
}
/////********************************************************************
HttpCOOKIE COOKIE &#61; new HttpCOOKIE("userinfo1");
COOKIE.Expires &#61; System.DateTime.Now.AddMinutes(2);//设置过期时间
for (int i &#61; 0; i < 20; i&#43;&#43;)
{
COOKIE.Values["Val" &#43;i.ToString()] &#61; i.ToString();
Response.COOKIEs.Add(COOKIE);
}
//读取 COOKIE 集合
for (int i &#61; 0; i < Request.COOKIEs.Count; i&#43;&#43;)
{
if (Request.COOKIEs.AllKeys[i] &#61;&#61; "userinfo1")
{
HttpCOOKIE COOKIEs &#61; Request.COOKIEs["userInfo1"];
Response.Write("name&#61;" &#43; COOKIEs.Name &#43; "
");
//Response.Write("name&#61;" &#43; COOKIEs.Value &#43; "
");
if (COOKIEs.HasKeys)//是否有子键
{
System.Collections.Specialized.NameValueCollection NameColl &#61; COOKIEs.Values;
for (int j &#61; 0; j < NameColl.Count; j&#43;&#43;)
{
Response.Write("子键名&#61;" &#43; NameColl.AllKeys[j] &#43; "
");
Response.Write("子键值&#61;" &#43; NameColl[j] &#43; "
");
}
}
else
{
Response.Write("value&#61;" &#43; COOKIEs.Value &#43; "
");
}
}
}
/////********************************************************************