System.Net.CookieContainer Class

Namespace: System.Net

Represents a collection of cookies that are associated with a particular domain.

This class is thread safe.

Syntax


public class CookieContainer : System.Net.ICookieContainer
            

Remarks

The CookieContainer class stores cookies for a domain. When you make an HTTP request, the CookieContainer checks for cookies that apply to the domain of the request and adds them to the request headers. When you receive an HTTP response, the CookieContainer checks for cookies in the response headers and stores them if they apply to the domain of the response.

You can use the CookieContainer to manage cookies for your application, such as storing authentication cookies or session cookies.

Constructors

Name Description
CookieContainer() Initializes a new instance of the CookieContainer class.
CookieContainer(int capacity) Initializes a new instance of the CookieContainer class with the specified capacity.
CookieContainer(int capacity, int perDomainCapacity, int maxCookieSize) Initializes a new instance of the CookieContainer class with the specified capacity, per-domain capacity, and maximum cookie size.

Properties

Name Description
Capacity Gets or sets the maximum number of cookies that can be stored in the CookieContainer.
Count Gets the number of cookies contained in the CookieContainer.
MaxCookieSize Gets or sets the maximum size, in bytes, of a single cookie.
PerDomainCapacity Gets or sets the maximum number of cookies that can be stored for a single domain.

Methods

Name Description
Add(Cookie cookie) Adds a Cookie object to the CookieContainer.
Add(Uri uri, Cookie cookie) Adds a Cookie object to the CookieContainer for the specified URI.
Add(Uri uri, string name, string value) Adds a cookie with the specified name and value to the CookieContainer for the specified URI.
GetCookies(Uri uri) Gets all the cookies associated with the specified URI from the CookieContainer.
SetCookies(CookieCollection cookies) Sets a collection of Cookie objects in the CookieContainer.

Example


using System;
using System.Net;

public class Example
{
    public static void Main()
    {
        // Create a CookieContainer
        CookieContainer cookieJar = new CookieContainer();

        // Create some cookies
        Uri exampleUri = new Uri("http://www.example.com/");
        Cookie authCookie = new Cookie("auth_token", "abcdef12345");
        Cookie sessionCookie = new Cookie("session_id", "xyz7890");

        // Add cookies to the container
        cookieJar.Add(exampleUri, authCookie);
        cookieJar.Add(exampleUri, sessionCookie);

        // Get cookies for a specific URI
        CookieCollection retrievedCookies = cookieJar.GetCookies(exampleUri);
        Console.WriteLine($"Cookies for {exampleUri}:");
        foreach (Cookie c in retrievedCookies)
        {
            Console.WriteLine($"- {c.Name}: {c.Value}");
        }

        // Create a web request and associate the cookie container
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.example.com/api/data");
        request.CookieContainer = cookieJar;

        // Send the request... (omitted for brevity)
        // Response handling would also potentially update the cookieJar
    }
}