Cookie 类

定义

提供一组用于管理 Cookie 的属性和方法。 此类不能被继承。

public ref class Cookie sealed
public sealed class Cookie
[System.Serializable]
public sealed class Cookie
type Cookie = class
[<System.Serializable>]
type Cookie = class
Public NotInheritable Class Cookie
继承
Cookie
属性

示例

以下示例向 URL 发送请求,并显示响应中返回的 Cookie。

using System.Net;
using System;
namespace Examples.System.Net.Cookies
{
    // This example is run at the command line.
    // Specify one argument: the name of the host to
    // send the request to.
    // If the request is sucessful, the example displays the contents of the cookies
    // returned by the host.

    public class CookieExample
    {
        public static void Main(string[] args)
        {
            if (args == null || args.Length != 1)
            {
                Console.WriteLine("Specify the URL to receive the request.");
                Environment.Exit(1);
            }
            var request = (HttpWebRequest)WebRequest.Create(args[0]);
            request.CookieContainer = new CookieContainer();

            using (var response = (HttpWebResponse) request.GetResponse())
            {
                // Print the properties of each cookie.
                foreach (Cookie cook in response.Cookies)
                {
                    Console.WriteLine("Cookie:");
                    Console.WriteLine($"{cook.Name} = {cook.Value}");
                    Console.WriteLine($"Domain: {cook.Domain}");
                    Console.WriteLine($"Path: {cook.Path}");
                    Console.WriteLine($"Port: {cook.Port}");
                    Console.WriteLine($"Secure: {cook.Secure}");

                    Console.WriteLine($"When issued: {cook.TimeStamp}");
                    Console.WriteLine($"Expires: {cook.Expires} (expired? {cook.Expired})");
                    Console.WriteLine($"Don't save: {cook.Discard}");
                    Console.WriteLine($"Comment: {cook.Comment}");
                    Console.WriteLine($"Uri for comments: {cook.CommentUri}");
                    Console.WriteLine($"Version: RFC {(cook.Version == 1 ? 2109 : 2965)}");

                    // Show the string representation of the cookie.
                    Console.WriteLine($"String: {cook}");
                }
            }
        }
    }
}

// Output from this example will be vary depending on the host name specified,
// but will be similar to the following.
/*
Cookie:
CustomerID = 13xyz
Domain: .contoso.com
Path: /
Port:
Secure: False
When issued: 1/14/2003 3:20:57 PM
Expires: 1/17/2013 11:14:07 AM (expired? False)
Don't save: False
Comment:
Uri for comments:
Version: RFC 2965
String: CustomerID = 13xyz
*/
Imports System.Net

' This example is run at the command line.
' Specify one argument: the name of the host to 
' receive the request.
' If the request is sucessful, the example displays the contents of the cookies
' returned by the host.

Public Class CookieExample
    
    Public Shared Sub Main(args() As String)
        If args Is Nothing OrElse args.Length <> 1 Then
            Console.WriteLine("Specify the URL to receive the request.")
            Environment.Exit(1)
        End If
        Dim request As HttpWebRequest = WebRequest.Create(args(0))
        request.CookieContainer = New CookieContainer()
    
        Using response As HttpWebResponse = request.GetResponse()
            ' Print the properties of each cookie.
            For Each cook As Cookie In response.Cookies
                Console.WriteLine("Cookie:")
                Console.WriteLine($"{cook.Name} = {cook.Value}")
                Console.WriteLine($"Domain: {cook.Domain}")
                Console.WriteLine($"Path: {cook.Path}")
                Console.WriteLine($"Port: {cook.Port}")
                Console.WriteLine($"Secure: {cook.Secure}")
    
                Console.WriteLine($"When issued: {cook.TimeStamp}")
                Console.WriteLine($"Expires: {cook.Expires} (expired? {cook.Expired})")
                Console.WriteLine($"Don't save: {cook.Discard}")
                Console.WriteLine($"Comment: {cook.Comment}")
                Console.WriteLine($"Uri for comments: {cook.CommentUri}")
                Console.WriteLine($"Version: RFC {If(cook.Version = 1, 2109, 2965)}")
    
                ' Show the string representation of the cookie.
                Console.WriteLine($"String: {cook}")
            Next
        End Using
    End Sub
End Class



' Output from this example will be vary depending on the host name specified,
' but will be similar to the following.
'
'Cookie:
'CustomerID = 13xyz
'Domain: .contoso.com
'Path: /
'Port:
'Secure: False
'When issued: 1/14/2003 3:20:57 PM
'Expires: 1/17/2013 11:14:07 AM (expired? False)
'Don't save: False
'Comment: 
'Uri for comments:
'Version: RFC 2965
'String: CustomerID = 13xyz
'

注解

客户端应用程序 Cookie 使用该类来检索使用 HTTP 响应接收的 Cookie 的相关信息。 在分析 HTTP 响应标头时支持以下 Cookie 格式:原始 Netscape 规范、RFC 2109 和 RFC 2965。

有关实例 Cookie的初始属性值列表,请参阅各种 Cookie 构造函数。

构造函数

名称 说明
Cookie()

初始化 Cookie 类的新实例。

Cookie(String, String, String, String)

使用指定的 CookieNameValuePath初始化 Domain 类的新实例。

Cookie(String, String, String)

使用指定的 CookieNameValue初始化 Path 类的新实例。

Cookie(String, String)

使用指定的 CookieName初始化 Value 类的新实例。

属性

名称 说明
Comment

获取或设置服务器可添加到的 Cookie注释。

CommentUri

获取或设置服务器可以提供的 CookieURI 注释。

Discard

获取或设置服务器设置的放弃标志。

Domain

获取或设置有效 Cookie URI。

Expired

获取或设置 . 的 Cookie当前状态。

Expires

获取或设置作为一种DateTime的过期日期和时间Cookie

HttpOnly

确定页面脚本或其他活动内容是否可以访问此 Cookie。

Name

获取或设置 . 的名称 Cookie

Path

获取或设置应用 URI Cookie

Port

获取或设置适用于的 Cookie TCP 端口的列表。

Secure

获取或设置一个 Cookie的安全级别。

TimeStamp

获取 Cookie 作为一个 DateTime颁发的时间。

Value

获取或设置 .ValueCookie

Version

获取或设置 Cookie 符合的 HTTP 状态维护的版本。

方法

名称 说明
Equals(Object)

Equals(Object)重写方法。

GetHashCode()

GetHashCode()重写方法。

GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ToString()

ToString()重写方法。

适用于

另请参阅