HttpCookieCollection 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供用于操作 HTTP cookie 的类型安全方式。
public ref class HttpCookieCollection sealed : System::Collections::Specialized::NameObjectCollectionBase
public sealed class HttpCookieCollection : System.Collections.Specialized.NameObjectCollectionBase
type HttpCookieCollection = class
inherit NameObjectCollectionBase
Public NotInheritable Class HttpCookieCollection
Inherits NameObjectCollectionBase
- 继承
示例
下面的代码示例演示如何使用Cookies对象的属性HttpRequest读取 Cookie,并使用对象的属性HttpResponse编写 CookieCookies。 这两个属性都返回 HttpCookieCollection 对象。 如果两个 Cookie 中有两个命名 userName
且 lastVisit
不在 HTTP 请求中,则会在 HTTP 响应中创建它们。 如果存在这两个 Cookie,则会显示 Cookie 的属性。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
// Check to see if cookies exist in HTTP request.
if (Request.Cookies["userName"] == null &&
Request.Cookies["lastVist"] == null)
{
Response.Cookies["userName"].Value = "user name";
Response.Cookies["userName"].Expires = DateTime.Now.AddMinutes(20d);
HttpCookie aCookie = new HttpCookie("lastVisit");
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddMinutes(20d);
Response.Cookies.Add(aCookie);
sb.Append("Two cookies added to response. " +
"Refresh the page to read the cookies.");
}
else
{
HttpCookieCollection cookies = Request.Cookies;
for (int i = 0; i < cookies.Count; i++)
{
sb.Append("Name: " + cookies[i].Name + "<br/>");
sb.Append("Value: " + cookies[i].Value + "<br/>");
sb.Append("Expires: " + cookies[i].Expires.ToString() +
"<br/><br/>");
}
}
Label1.Text = sb.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>HttpCookieCollection Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim sb As New StringBuilder()
' Check to see if cookies exist in HTTP request.
If (Request.Cookies("userName") Is Nothing AndAlso _
Request.Cookies("lastVisit") Is Nothing) Then
Response.Cookies("userName").Value = "user name"
Response.Cookies("userName").Expires = DateTime.Now.AddMinutes(20D)
Dim aCookie As HttpCookie
aCookie = New HttpCookie("lastVisit")
aCookie.Value = DateTime.Now.ToString()
aCookie.Expires = DateTime.Now.AddMinutes(20D)
Response.Cookies.Add(aCookie)
sb.Append("Two cookies added to response. " & _
"Refresh the page to read the cookies.")
Else
Dim cookies As HttpCookieCollection
cookies = Request.Cookies
For i As Integer = 0 To cookies.Count - 1
sb.Append("Name: " & cookies(i).Name & "<br/>")
sb.Append("Value: " & cookies(i).Value & "<br/>")
sb.Append("Expires: " & cookies(i).Expires.ToString() & _
"<br/><br/>")
Next
End If
Label1.Text = sb.ToString()
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>HttpCookieCollection Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
构造函数
HttpCookieCollection() |
初始化 HttpCookieCollection 类的新实例。 |
属性
AllKeys |
获取一个字符串数组,该数组包含此 Cookie 集合中的所有键(Cookie 名称)。 |
Count |
获取包含在 NameObjectCollectionBase 实例中的键/值对的数目。 (继承自 NameObjectCollectionBase) |
IsReadOnly |
获取或设置一个值,通过该值指示 NameObjectCollectionBase 实例是否为只读的。 (继承自 NameObjectCollectionBase) |
Item[Int32] |
从 Cookie 集合中获取具有指定数字索引的 Cookie。 |
Item[String] |
从 Cookie 集合中获取具有指定名称的 Cookie。 |
Keys |
获取包含 NameObjectCollectionBase.KeysCollection 实例中所有键的 NameObjectCollectionBase 实例。 (继承自 NameObjectCollectionBase) |
方法
显式接口实现
ICollection.CopyTo(Array, Int32) |
从目标数组的指定索引处开始将整个 NameObjectCollectionBase 复制到兼容的一维 Array。 (继承自 NameObjectCollectionBase) |
ICollection.IsSynchronized |
获取一个值,该值指示对 NameObjectCollectionBase 对象的访问是否同步(线程安全)。 (继承自 NameObjectCollectionBase) |
ICollection.SyncRoot |
获取一个对象,该对象可用于同步对 NameObjectCollectionBase 对象的访问。 (继承自 NameObjectCollectionBase) |
扩展方法
Cast<TResult>(IEnumerable) |
将 IEnumerable 的元素强制转换为指定的类型。 |
OfType<TResult>(IEnumerable) |
根据指定类型筛选 IEnumerable 的元素。 |
AsParallel(IEnumerable) |
启用查询的并行化。 |
AsQueryable(IEnumerable) |
将 IEnumerable 转换为 IQueryable。 |