HttpCookieCollection 类

定义

提供用于操作 HTTP cookie 的类型安全方式。Provides a type-safe way to manipulate HTTP cookies.

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
继承
HttpCookieCollection

示例

下面的代码示例演示如何使用对象的属性读取 cookie Cookies HttpRequest ,并使用对象的属性写入 cookie Cookies HttpResponseThe following code example demonstrates how to read cookies using the Cookies property of the HttpRequest object and write cookies using the Cookies property of the HttpResponse object. 这两个属性都返回 HttpCookieCollection 对象。Both properties return HttpCookieCollection objects. 如果两个 cookie 中有一个名为 userNamelastVisit 不在 http 请求中,则会在 http 响应中创建它们。If either of two cookies named userName and lastVisit are not in the HTTP request, then they are created in the HTTP response. 如果两个 cookie 存在,则会显示 cookie 的属性。If the two cookies exist, the properties of the cookies are displayed.

<%@ 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 类的新实例。Initializes a new instance of the HttpCookieCollection class.

属性

AllKeys

获取一个字符串数组,该数组包含此 Cookie 集合中的所有键(Cookie 名称)。Gets a string array containing all the keys (cookie names) in the cookie collection.

Count

获取包含在 NameObjectCollectionBase 实例中的键/值对的数目。Gets the number of key/value pairs contained in the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
IsReadOnly

获取或设置一个值,通过该值指示 NameObjectCollectionBase 实例是否为只读的。Gets or sets a value indicating whether the NameObjectCollectionBase instance is read-only.

(继承自 NameObjectCollectionBase)
Item[Int32]

从 Cookie 集合中获取具有指定数字索引的 Cookie。Gets the cookie with the specified numerical index from the cookie collection.

Item[String]

从 Cookie 集合中获取具有指定名称的 Cookie。Gets the cookie with the specified name from the cookie collection.

Keys

获取包含 NameObjectCollectionBase.KeysCollection 实例中所有键的 NameObjectCollectionBase 实例。Gets a NameObjectCollectionBase.KeysCollection instance that contains all the keys in the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)

方法

Add(HttpCookie)

将指定的 Cookie 添加到此 Cookie 集合中。Adds the specified cookie to the cookie collection.

BaseAdd(String, Object)

将具有指定键和值的项添加到 NameObjectCollectionBase 实例中。Adds an entry with the specified key and value into the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseClear()

移除 NameObjectCollectionBase 实例中的所有项。Removes all entries from the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseGet(Int32)

获取 NameObjectCollectionBase 实例的指定索引处的项值。Gets the value of the entry at the specified index of the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseGet(String)

获取 NameObjectCollectionBase 实例中第一个具有指定键的项值。Gets the value of the first entry with the specified key from the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseGetAllKeys()

返回 String 数组,该数组包含 NameObjectCollectionBase 实例中的所有键。Returns a String array that contains all the keys in the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseGetAllValues()

返回 Object 数组,该数组包含 NameObjectCollectionBase 实例中的所有值。Returns an Object array that contains all the values in the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseGetAllValues(Type)

返回指定类型的数组,该数组包含 NameObjectCollectionBase 实例中的所有值。Returns an array of the specified type that contains all the values in the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseGetKey(Int32)

获取 NameObjectCollectionBase 实例的指定索引处的项键。Gets the key of the entry at the specified index of the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseHasKeys()

获取一个值,通过该值指示 NameObjectCollectionBase 实例是否包含键不为 null 的项。Gets a value indicating whether the NameObjectCollectionBase instance contains entries whose keys are not null.

(继承自 NameObjectCollectionBase)
BaseRemove(String)

移除 NameObjectCollectionBase 实例中具有指定键的项。Removes the entries with the specified key from the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseRemoveAt(Int32)

移除 NameObjectCollectionBase 实例的指定索引处的项。Removes the entry at the specified index of the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseSet(Int32, Object)

设置 NameObjectCollectionBase 实例的指定索引处的项值。Sets the value of the entry at the specified index of the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
BaseSet(String, Object)

NameObjectCollectionBase 实例中第一个具有指定键的项设置值(如果有这样的项);否则将具有指定键和值的项添加到 NameObjectCollectionBase 实例中。Sets the value of the first entry with the specified key in the NameObjectCollectionBase instance, if found; otherwise, adds an entry with the specified key and value into the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
Clear()

清除 Cookie 集合中的所有 Cookie。Clears all cookies from the cookie collection.

CopyTo(Array, Int32)

从指定的数组索引处开始,将 Cookie 集合的成员复制到 Array 中。Copies members of the cookie collection to an Array beginning at the specified index of the array.

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
Get(Int32)

从 Cookie 集合中返回具有指定索引的 HttpCookie 项。Returns the HttpCookie item with the specified index from the cookie collection.

Get(String)

从 Cookie 集合中返回具有指定名称的 Cookie。Returns the cookie with the specified name from the cookie collection.

GetEnumerator()

返回遍历 NameObjectCollectionBase 的枚举器。Returns an enumerator that iterates through the NameObjectCollectionBase.

(继承自 NameObjectCollectionBase)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetKey(Int32)

返回指定数字索引处的 Cookie 键(名称)。Returns the key (name) of the cookie at the specified numerical index.

GetObjectData(SerializationInfo, StreamingContext)

实现 ISerializable 接口,并返回序列化 NameObjectCollectionBase 实例所需的数据。Implements the ISerializable interface and returns the data needed to serialize the NameObjectCollectionBase instance.

(继承自 NameObjectCollectionBase)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
OnDeserialization(Object)

实现 ISerializable 接口,并在完成反序列化之后引发反序列化事件。Implements the ISerializable interface and raises the deserialization event when the deserialization is complete.

(继承自 NameObjectCollectionBase)
Remove(String)

从集合中移除具有指定名称的 Cookie。Removes the cookie with the specified name from the collection.

Set(HttpCookie)

更新 Cookie 集合中现有 Cookie 的值。Updates the value of an existing cookie in a cookie collection.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

显式接口实现

ICollection.CopyTo(Array, Int32)

从目标数组的指定索引处开始将整个 NameObjectCollectionBase 复制到兼容的一维 ArrayCopies the entire NameObjectCollectionBase to a compatible one-dimensional Array, starting at the specified index of the target array.

(继承自 NameObjectCollectionBase)
ICollection.IsSynchronized

获取一个值,该值指示对 NameObjectCollectionBase 对象的访问是否同步(线程安全)。Gets a value indicating whether access to the NameObjectCollectionBase object is synchronized (thread safe).

(继承自 NameObjectCollectionBase)
ICollection.SyncRoot

获取一个对象,该对象可用于同步对 NameObjectCollectionBase 对象的访问。Gets an object that can be used to synchronize access to the NameObjectCollectionBase object.

(继承自 NameObjectCollectionBase)

扩展方法

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于

另请参阅