HttpResponse.Cookies HttpResponse.Cookies HttpResponse.Cookies HttpResponse.Cookies Property

定义

获取响应 cookie 集合。Gets the response cookie collection.

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

属性值

响应 cookie 集合。The response cookie collection.

示例

下面的示例创建一个名为LastVisit的新 cookie,将该 cookie 的值设置为当前日期和时间,并将该 cookie 添加到当前 cookie 集合中。The following example creates a new cookie named LastVisit, sets the value of the cookie to the current date and time, and adds the cookie to the current cookie collection. Cookie 集合中的所有 cookie 都通过 HTTP 输出流发送到Set-Cookie标头中的客户端。All cookies in the cookie collection are sent to the client in the Set-Cookie header with the HTTP output stream.

HttpCookie MyCookie = new HttpCookie("LastVisit");
DateTime now = DateTime.Now;

MyCookie.Value = now.ToString();
MyCookie.Expires = now.AddHours(1);

Response.Cookies.Add(MyCookie);
   
Dim MyCookie As New HttpCookie("LastVisit")
Dim now As DateTime = DateTime.Now

MyCookie.Value = now.ToString()
MyCookie.Expires = now.AddHours(1)

Response.Cookies.Add(MyCookie)
   

注解

ASP.NET 包括两个内部 cookie 集合。ASP.NET includes two intrinsic cookie collections. 通过的Cookies Cookie集合访问的集合包含客户端向标头中的服务器传输的cookie。HttpRequestThe collection accessed through the Cookies collection of HttpRequest contains cookies transmitted by the client to the server in the Cookie header. 通过Cookies Set-CookieHttpResponse集合访问的集合包含在服务器上创建并在标头中传输到客户端的新 cookie。The collection accessed through the Cookies collection of HttpResponse contains new cookies created on the server and transmitted to the client in the Set-Cookie header.

使用HttpResponse.Cookies集合添加 cookie 后,cookie 会立即出现HttpRequest.Cookies在集合中,即使尚未将响应发送到客户端也是如此。After you add a cookie by using the HttpResponse.Cookies collection, the cookie is immediately available in the HttpRequest.Cookies collection, even if the response has not been sent to the client.

适用于