CookieContainer.Add Method

Definition

Adds a Cookie to a CookieContainer.

Overloads

Add(Cookie)

Adds a Cookie to a CookieContainer. This method uses the domain from the Cookie to determine which domain collection to associate the Cookie with.

Add(CookieCollection)

Adds the contents of a CookieCollection to the CookieContainer.

Add(Uri, Cookie)

Adds a Cookie to the CookieContainer for a particular URI.

Add(Uri, CookieCollection)

Adds the contents of a CookieCollection to the CookieContainer for a particular URI.

Add(Cookie)

Source:
CookieContainer.cs
Source:
CookieContainer.cs
Source:
CookieContainer.cs

Adds a Cookie to a CookieContainer. This method uses the domain from the Cookie to determine which domain collection to associate the Cookie with.

C#
public void Add(System.Net.Cookie cookie);

Parameters

cookie
Cookie

The Cookie to be added to the CookieContainer.

Exceptions

cookie is null.

The domain for cookie is null or the empty string ("").

cookie is larger than maxCookieSize.

-or-

the domain for cookie is not a valid URI.

Remarks

If the Count property equals or exceeds the Capacity property, one or more Cookie instances are removed from the container before adding the cookie parameter. Enough Cookie instances are removed to bring Count below Capacity as follows: if there are expired instances in the given scope, they are cleaned up. If not, then the least recently used CookieCollection is found and removed from the container.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(CookieCollection)

Source:
CookieContainer.cs
Source:
CookieContainer.cs
Source:
CookieContainer.cs

Adds the contents of a CookieCollection to the CookieContainer.

C#
public void Add(System.Net.CookieCollection cookies);

Parameters

cookies
CookieCollection

The CookieCollection to be added to the CookieContainer.

Exceptions

cookies is null.

Remarks

If the Count property equals the Capacity property, one or more Cookie instances are removed from the container before adding the contents of the cookies parameter. Enough Cookie instances are removed to make room for cookies as follows: if there are expired instances, they are cleaned up. If not, or if more room is needed, then the least recently used CookieCollection is found and removed from the container.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Add(Uri, Cookie)

Source:
CookieContainer.cs
Source:
CookieContainer.cs
Source:
CookieContainer.cs

Adds a Cookie to the CookieContainer for a particular URI.

C#
public void Add(Uri uri, System.Net.Cookie cookie);

Parameters

uri
Uri

The URI of the Cookie to be added to the CookieContainer.

cookie
Cookie

The Cookie to be added to the CookieContainer.

Exceptions

uri is null or cookie is null.

cookie is larger than maxCookieSize.

-or-

The domain for cookie is not a valid URI.

Remarks

If you add a Cookie instance for just one specific host, do not set the Domain property of the Cookie instance. This is set automatically, based on the URI.

If your URI corresponds to your local domain and sends to all the hosts on the local domain, set the CookieDomain property equal to ".local". Otherwise, make sure it matches the host name used in the URI.

If the Version property of a Cookie is Netscape, the Path property of the Cookie, if not set explicitly, is derived from the URI and is the complete path from the URI, including the page name.

If the Count property equals the Capacity property, one or more Cookie instances are removed from the container before adding the cookie parameter. Enough Cookie instances are removed to bring Count below Capacity as follows: if there are expired instances in scope, they are cleaned up. If not, then the least recently used CookieCollection is found and removed from the container.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Add(Uri, CookieCollection)

Source:
CookieContainer.cs
Source:
CookieContainer.cs
Source:
CookieContainer.cs

Adds the contents of a CookieCollection to the CookieContainer for a particular URI.

C#
public void Add(Uri uri, System.Net.CookieCollection cookies);

Parameters

uri
Uri

The URI of the CookieCollection to be added to the CookieContainer.

cookies
CookieCollection

The CookieCollection to be added to the CookieContainer.

Exceptions

cookies is null.

The domain for one of the cookies in cookies is null.

One of the cookies in cookies contains an invalid domain.

Remarks

If you add a Cookie instance for just one specific host, do not set the Domain property of the Cookie instance. This is set automatically, based on the URI.

If your URI corresponds to your local domain and sends to all the hosts on the local domain, set the CookieDomain property equal to ".local". Otherwise, make sure it matches the host name used in the URI.

If Count equals Capacity, one or more Cookie instances is removed from the container before adding the cookie parameter. Enough Cookie instances are removed to bring Count below Capacity as follows: if there are expired instances in scope they are cleaned up. If not, then the least recently used CookieCollection is found and removed from the container.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0