HttpCookieCollection HttpCookieCollection HttpCookieCollection HttpCookieCollection Class

定義

提供類型安全的方式來操作 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
繼承
HttpCookieCollectionHttpCookieCollectionHttpCookieCollectionHttpCookieCollection

範例

下列程式碼Cookies範例示範如何使用HttpRequest物件的屬性讀取 cookie, 並HttpResponse使用物件的Cookies屬性來寫入 cookie。The 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. 如果名為userNamelastVisit的兩個 cookie 之一不在 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() HttpCookieCollection() HttpCookieCollection()

初始化 HttpCookieCollection 類別的新執行個體。Initializes a new instance of the HttpCookieCollection class.

屬性

AllKeys AllKeys AllKeys AllKeys

取得包含 Cookie 集合中所有索引鍵 (Cookie 名稱) 的字串陣列。Gets a string array containing all the keys (cookie names) in the cookie collection.

Count Count Count Count

取得 NameObjectCollectionBase 執行個體中包含的索引鍵/值組數目。Gets the number of key/value pairs contained in the NameObjectCollectionBase instance.

(Inherited from NameObjectCollectionBase)
ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

取得值,表示是否要同步處理 (執行緒安全) 對 NameObjectCollectionBase 物件的存取。Gets a value indicating whether access to the NameObjectCollectionBase object is synchronized (thread safe).

(Inherited from NameObjectCollectionBase)
ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

取得可用來同步處理對 NameObjectCollectionBase 物件之存取的物件。Gets an object that can be used to synchronize access to the NameObjectCollectionBase object.

(Inherited from NameObjectCollectionBase)
IsReadOnly IsReadOnly IsReadOnly IsReadOnly

取得或設定值,表示 NameObjectCollectionBase 執行個體是否為唯讀。Gets or sets a value indicating whether the NameObjectCollectionBase instance is read-only.

(Inherited from NameObjectCollectionBase)
Item[Int32] Item[Int32] Item[Int32] Item[Int32]

以指定的數值索引從 Cookie 集合取得 Cookie。Gets the cookie with the specified numerical index from the cookie collection.

Item[String] Item[String] Item[String] Item[String]

以指定的名稱從 Cookie 集合取得 Cookie。Gets the cookie with the specified name from the cookie collection.

Keys Keys Keys Keys

取得 NameObjectCollectionBase.KeysCollection 執行個體,其中包含 NameObjectCollectionBase 執行個體中的所有索引鍵。Gets a NameObjectCollectionBase.KeysCollection instance that contains all the keys in the NameObjectCollectionBase instance.

(Inherited from NameObjectCollectionBase)

方法

Add(HttpCookie) Add(HttpCookie) Add(HttpCookie) Add(HttpCookie)

將指定的 Cookie 加入 Cookie 集合中。Adds the specified cookie to the cookie collection.

BaseAdd(String, Object) BaseAdd(String, Object) BaseAdd(String, Object) BaseAdd(String, Object)

將具有指定索引鍵和值的項目加入 NameObjectCollectionBase 執行個體。Adds an entry with the specified key and value into the NameObjectCollectionBase instance.

(Inherited from NameObjectCollectionBase)
BaseClear() BaseClear() BaseClear() BaseClear()

將所有項目從 NameObjectCollectionBase 執行個體中移除。Removes all entries from the NameObjectCollectionBase instance.

(Inherited from NameObjectCollectionBase)
BaseGet(Int32) BaseGet(Int32) BaseGet(Int32) BaseGet(Int32)

取得 NameObjectCollectionBase 執行個體指定索引處之項目的值。Gets the value of the entry at the specified index of the NameObjectCollectionBase instance.

(Inherited from NameObjectCollectionBase)
BaseGet(String) BaseGet(String) BaseGet(String) BaseGet(String)

NameObjectCollectionBase 執行個體取得具有指定索引鍵之第一個項目的值。Gets the value of the first entry with the specified key from the NameObjectCollectionBase instance.

(Inherited from NameObjectCollectionBase)
BaseGetAllKeys() BaseGetAllKeys() BaseGetAllKeys() BaseGetAllKeys()

傳回 String 陣列,其中包含 NameObjectCollectionBase 執行個體中的所有索引鍵。Returns a String array that contains all the keys in the NameObjectCollectionBase instance.

(Inherited from NameObjectCollectionBase)
BaseGetAllValues() BaseGetAllValues() BaseGetAllValues() BaseGetAllValues()

傳回 Object 陣列,其中包含 NameObjectCollectionBase 執行個體中的所有值。Returns an Object array that contains all the values in the NameObjectCollectionBase instance.

(Inherited from NameObjectCollectionBase)
BaseGetAllValues(Type) BaseGetAllValues(Type) BaseGetAllValues(Type) BaseGetAllValues(Type)

傳回指定類型的陣列,其中包含 NameObjectCollectionBase 執行個體中的所有值。Returns an array of the specified type that contains all the values in the NameObjectCollectionBase instance.

(Inherited from NameObjectCollectionBase)
BaseGetKey(Int32) BaseGetKey(Int32) BaseGetKey(Int32) BaseGetKey(Int32)

取得 NameObjectCollectionBase 執行個體指定索引處之項目的索引鍵。Gets the key of the entry at the specified index of the NameObjectCollectionBase instance.

(Inherited from NameObjectCollectionBase)
BaseHasKeys() BaseHasKeys() BaseHasKeys() BaseHasKeys()

取得值,表示 NameObjectCollectionBase 執行個體是否包含其索引鍵不是 null 的項目。Gets a value indicating whether the NameObjectCollectionBase instance contains entries whose keys are not null.

(Inherited from NameObjectCollectionBase)
BaseRemove(String) BaseRemove(String) BaseRemove(String) BaseRemove(String)

將具有指定索引鍵的項目從 NameObjectCollectionBase 中移除。Removes the entries with the specified key from the NameObjectCollectionBase instance.

(Inherited from NameObjectCollectionBase)
BaseRemoveAt(Int32) BaseRemoveAt(Int32) BaseRemoveAt(Int32) BaseRemoveAt(Int32)

移除 NameObjectCollectionBase 執行個體指定索引處的項目。Removes the entry at the specified index of the NameObjectCollectionBase instance.

(Inherited from NameObjectCollectionBase)
BaseSet(Int32, Object) BaseSet(Int32, Object) BaseSet(Int32, Object) BaseSet(Int32, Object)

設定 NameObjectCollectionBase 執行個體指定索引處之項目的值。Sets the value of the entry at the specified index of the NameObjectCollectionBase instance.

(Inherited from NameObjectCollectionBase)
BaseSet(String, Object) BaseSet(String, Object) BaseSet(String, Object) 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.

(Inherited from NameObjectCollectionBase)
Clear() Clear() Clear() Clear()

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

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

將 Cookie 集合成員複製到以指定的陣列索引起始的 ArrayCopies members of the cookie collection to an Array beginning at the specified index of the array.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
Get(Int32) Get(Int32) Get(Int32) Get(Int32)

從 Cookie 集合傳回具有指定之索引的 HttpCookie 物件。Returns the HttpCookie item with the specified index from the cookie collection.

Get(String) Get(String) Get(String) Get(String)

從 Cookie 集合傳回指定之名稱的 Cookie。Returns the cookie with the specified name from the cookie collection.

GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

傳回在 NameObjectCollectionBase 中逐一查看的列舉值。Returns an enumerator that iterates through the NameObjectCollectionBase.

(Inherited from NameObjectCollectionBase)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetKey(Int32) GetKey(Int32) GetKey(Int32) GetKey(Int32)

傳回在指定數值索引的 Cookie 索引鍵 (名稱)。Returns the key (name) of the cookie at the specified numerical index.

GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

實作 ISerializable 介面,並傳回序列化 NameObjectCollectionBase 執行個體所需的資料。Implements the ISerializable interface and returns the data needed to serialize the NameObjectCollectionBase instance.

(Inherited from NameObjectCollectionBase)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
OnDeserialization(Object) OnDeserialization(Object) OnDeserialization(Object) OnDeserialization(Object)

實作 ISerializable 介面,並於還原序列化完成時引發還原序列化事件。Implements the ISerializable interface and raises the deserialization event when the deserialization is complete.

(Inherited from NameObjectCollectionBase)
Remove(String) Remove(String) Remove(String) Remove(String)

以指定名稱從集合移除 Cookie。Removes the cookie with the specified name from the collection.

Set(HttpCookie) Set(HttpCookie) Set(HttpCookie) Set(HttpCookie)

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

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

明確介面實作

ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32)

從目標陣列的指定索引開始,將整個 NameObjectCollectionBase 複製到相容的一維 ArrayCopies the entire NameObjectCollectionBase to a compatible one-dimensional Array, starting at the specified index of the target array.

(Inherited from NameObjectCollectionBase)

擴充方法

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於

另請參閱