Cookie Constructors

Definition

Initializes a new instance of the Cookie class. In general, an application does not need to construct a Cookie class because it is created automatically based on the Set-Cookie header received with the HTTP response.

Overloads

Cookie()

Initializes a new instance of the Cookie class.

Cookie(String, String)

Initializes a new instance of the Cookie class with a specified Name and Value.

Cookie(String, String, String)

Initializes a new instance of the Cookie class with a specified Name, Value, and Path.

Cookie(String, String, String, String)

Initializes a new instance of the Cookie class with a specified Name, Value, Path, and Domain.

Initializes a new instance of the Cookie class.

public:
 Cookie();
public Cookie ();
Public Sub New ()

Remarks

The parameterless constructor initializes all fields to their default values, using empty strings ("") for name, value, path, and domain. Note that at least the Name property must be initialized before using an instance of the Cookie class.

Applies to

Initializes a new instance of the Cookie class with a specified Name and Value.

public:
 Cookie(System::String ^ name, System::String ^ value);
public Cookie (string name, string value);
public Cookie (string name, string? value);
new System.Net.Cookie : string * string -> System.Net.Cookie
Public Sub New (name As String, value As String)

Parameters

name
String

The name of a Cookie. The following characters must not be used inside name: equal sign, semicolon, comma, newline (\n), return (\r), tab (\t), and space character. The dollar sign character ("$") cannot be the first character.

value
String

The value of a Cookie. The following characters must not be used inside value: semicolon, comma.

Exceptions

The name parameter is null.

-or-

The name parameter is of zero length.

-or-

The name parameter contains an invalid character.

-or-

The value parameter is null .

-or -

The value parameter contains a string not enclosed in quotes that contains an invalid character.

Remarks

The default for the value parameter uses the empty string ("").

The value parameter for a Cookie must not be a null reference (Nothing in Visual Basic). The semicolon (";") and comma (",") characters are reserved and cannot be passed in the value parameter unless the string passed in the value parameter is enclosed in double quotes. So the following example constructor would succeed, but when you try to add this Cookie to a CookieContainer instance with the Add(Cookie) or Add(Uri, Cookie) methods, the operation will fail and throw an exception:

System.Net.Cookie cookie = new System.Net.Cookie("contoso", "123,456");
cookie.Domain = "contoso.com";
new CookieContainer().Add(cookie);

However, the following constructor with these special characters escaped will create a Cookie that can be added to a CookieContainer instance:

System.Net.Cookie cookie = new System.Net.Cookie("contoso", "\"123,456\"");
cookie.Domain = "contoso.com";
new CookieContainer().Add(cookie);

The comma character is used as a delimiter between separate cookies on the same line.

See also

Applies to

Initializes a new instance of the Cookie class with a specified Name, Value, and Path.

public:
 Cookie(System::String ^ name, System::String ^ value, System::String ^ path);
public Cookie (string name, string value, string path);
public Cookie (string name, string? value, string? path);
new System.Net.Cookie : string * string * string -> System.Net.Cookie
Public Sub New (name As String, value As String, path As String)

Parameters

name
String

The name of a Cookie. The following characters must not be used inside name: equal sign, semicolon, comma, newline (\n), return (\r), tab (\t), and space character. The dollar sign character ("$") cannot be the first character.

value
String

The value of a Cookie. The following characters must not be used inside value: semicolon, comma.

path
String

The subset of URIs on the origin server to which this Cookie applies. The default value is "/".

Exceptions

The name parameter is null.

-or-

The name parameter is of zero length.

-or-

The name parameter contains an invalid character.

-or-

The value parameter is null .

-or -

The value parameter contains a string not enclosed in quotes that contains an invalid character.

Remarks

The default for the path parameter uses the empty string ("").

The value parameter for a Cookie must not be a null reference (Nothing in Visual Basic). The semicolon (";") and comma (",") characters are reserved and cannot be passed in the value parameter unless the string passed in the value parameter is enclosed in double quotes. So the following example constructor would succeed, but when you try to add this Cookie to a CookieContainer instance with the Add(Cookie) or Add(Uri, Cookie) methods, the operation will fail and throw an exception:

System.Net.Cookie cookie = new System.Net.Cookie("contoso", "123,456", "");
cookie.Domain = "contoso.com";
new CookieContainer().Add(cookie);

However, the following constructor with these special characters escaped will create a Cookie that can be added to a CookieContainer instance:

System.Net.Cookie cookie = new System.Net.Cookie("contoso", "\"123,456\"", "");
cookie.Domain = "contoso.com";
new CookieContainer().Add(cookie);

The comma character is used as a delimiter between separate cookies on the same line.

See also

Applies to

Initializes a new instance of the Cookie class with a specified Name, Value, Path, and Domain.

public:
 Cookie(System::String ^ name, System::String ^ value, System::String ^ path, System::String ^ domain);
public Cookie (string name, string value, string path, string domain);
public Cookie (string name, string? value, string? path, string? domain);
new System.Net.Cookie : string * string * string * string -> System.Net.Cookie
Public Sub New (name As String, value As String, path As String, domain As String)

Parameters

name
String

The name of a Cookie. The following characters must not be used inside name: equal sign, semicolon, comma, newline (\n), return (\r), tab (\t), and space character. The dollar sign character ("$") cannot be the first character.

value
String

The value of a Cookie object. The following characters must not be used inside value: semicolon, comma.

path
String

The subset of URIs on the origin server to which this Cookie applies. The default value is "/".

domain
String

The optional internet domain for which this Cookie is valid. The default value is the host this Cookie has been received from.

Exceptions

The name parameter is null.

-or-

The name parameter is of zero length.

-or-

The name parameter contains an invalid character.

-or-

The value parameter is null .

-or -

The value parameter contains a string not enclosed in quotes that contains an invalid character.

Remarks

The default for the domain and path parameters uses the empty string ("").

The value parameter for a Cookie must not be a null reference (Nothing in Visual Basic). The semicolon (";") and comma (",") characters are reserved and cannot be passed in the value parameter unless the string passed in the value parameter is enclosed in double quotes. So the following example constructor would succeed, but when you try to add this Cookie to a CookieContainer instance with the Add(Cookie) or Add(Uri, Cookie) methods, the operation will fail and throw an exception:

System.Net.Cookie cookie = new System.Net.Cookie("contoso", "123,456", "", "contoso.com");
new CookieContainer().Add(cookie);

However, the following constructor with these special characters escaped will create a Cookie that can be added to a CookieContainer instance:

System.Net.Cookie cookie = new System.Net.Cookie("contoso", "\"123,456\"", "", "contoso.com");
new CookieContainer().Add(cookie);

The comma character is used as a delimiter between separate cookies on the same line.

See also

Applies to