共用方式為


HttpRequest.Cookies 屬性

定義

取得用戶端送出的 Cookie 的集合。

public:
 property System::Web::HttpCookieCollection ^ Cookies { System::Web::HttpCookieCollection ^ get(); };
public System.Web.HttpCookieCollection Cookies { get; }
member this.Cookies : System.Web.HttpCookieCollection
Public ReadOnly Property Cookies As HttpCookieCollection

屬性值

HttpCookieCollection

HttpCookieCollection 物件,代表用戶端的 Cookie 變數。

範例

下列程式碼範例會迴圈查看用戶端傳送的所有 Cookie,並將每個 Cookie 的名稱、到期日、安全性參數和值傳送至 HTTP 輸出。

int loop1, loop2;
HttpCookieCollection MyCookieColl;
HttpCookie MyCookie;

MyCookieColl = Request.Cookies;

// Capture all cookie names into a string array.
String[] arr1 = MyCookieColl.AllKeys;

// Grab individual cookie objects by cookie name.
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
   MyCookie = MyCookieColl[arr1[loop1]];
   Response.Write("Cookie: " + MyCookie.Name + "<br>");
   Response.Write ("Secure:" + MyCookie.Secure + "<br>");

   //Grab all values for single cookie into an object array.
   String[] arr2 = MyCookie.Values.AllKeys;

   //Loop through cookie Value collection and print all values.
   for (loop2 = 0; loop2 < arr2.Length; loop2++)
   {
      Response.Write("Value" + loop2 + ": " + Server.HtmlEncode(arr2[loop2]) + "<br>");
   }
}

Dim loop1, loop2 As Integer
Dim arr1(), arr2() As String
Dim MyCookieColl As HttpCookieCollection 
Dim MyCookie As HttpCookie

MyCookieColl = Request.Cookies
' Capture all cookie names into a string array.
arr1 = MyCookieColl.AllKeys
' Grab individual cookie objects by cookie name     
for loop1 = 0 To arr1.GetUpperBound(0)
   MyCookie = MyCookieColl(arr1(loop1))
   Response.Write("Cookie: " & MyCookie.Name & "<br>")
           Response.Write("Secure:" & MyCookie.Secure & "<br>")

   ' Grab all values for single cookie into an object array.
   arr2 = MyCookie.Values.AllKeys
   ' Loop through cookie value collection and print all values.
   for loop2 = 0 To arr2.GetUpperBound(0)
      Response.Write("Value " & CStr(loop2) + ": " & Server.HtmlEncode(arr2(loop2)) & "<br>")
   Next loop2
Next loop1
  

備註

ASP.NET 包含兩個內建 Cookie 集合。 透過 集合 HttpRequest 存取的 Cookies 集合包含用戶端傳送至標頭中伺服器所傳輸的 Cookie Cookie。 透過 集合 HttpResponse 存取的 Cookies 集合包含伺服器上建立的新 Cookie,並傳輸至標頭中的 Set-Cookie 用戶端。

注意

使用 HttpResponse.Cookies 集合新增 Cookie 之後,即使尚未將回應傳送至用戶端,該 Cookie 仍可立即在集合中使用 HttpRequest.Cookies

適用於

另請參閱