This documentation is archived and is not being maintained.

HttpListenerResponse.SetCookie Method

Adds or updates a Cookie in the collection of cookies sent with this response.

Namespace: System.Net
Assembly: System (in system.dll)

public void SetCookie (
	Cookie cookie
)
public void SetCookie (
	Cookie cookie
)
public function SetCookie (
	cookie : Cookie
)
Not applicable.

Parameters

cookie

A Cookie for this response.

Exception typeCondition

ArgumentNullException

cookie is a null reference (Nothing in Visual Basic).

ArgumentException

The cookie already exists in the collection and could not be replaced.

Two cookies are considered the same if the values of their Name, Domain, and Path properties are the same. If these three pieces of information are the same, the method attempts to update the cookie. The name and domain comparisons are not case sensitive, but the path comparison is case sensitive.

The following code example demonstrates calling this method.

// This example requires the System and System.Net namespaces.
public static void SimpleCookieExample(string[] prefixes)
{
    // Create a listener.
    HttpListener listener = new HttpListener();
    // Add the prefixes.
    foreach (string s in prefixes)
    {
        listener.Prefixes.Add(s);
    }
    listener.Start();
    Console.WriteLine("Listening...");
    // Note: The GetContext method blocks while waiting for a request. 
    HttpListenerContext context = listener.GetContext();
    HttpListenerRequest request = context.Request;
    
    // This application sends a cookie to the client marking the time 
    // they visited. 
    Cookie timeStampCookie = new Cookie("VisitDate", DateTime.Now.ToString());
    // Obtain a response object.
    HttpListenerResponse response = context.Response;
    // Add the cookie to the response.
    response.SetCookie(timeStampCookie);
    // Construct a response.
    string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
    response.ContentEncoding = System.Text.Encoding.UTF8;
    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
    // Send the response.
    response.Close(buffer, true);
    listener.Stop();
}

// This example requires the System and System.Net namespaces.
public static void SimpleCookieExample(String prefixes[])
{
    // Create a listener.
    HttpListener listener = new HttpListener();
    // Add the prefixes.
    for (int iCtr = 0; iCtr < prefixes.get_Length(); iCtr++) {
        String s = prefixes[iCtr];
        listener.get_Prefixes().Add(s);
    }
    listener.Start();
    Console.WriteLine("Listening...");
    // Note: The GetContext method blocks while waiting for a request. 
    HttpListenerContext context = listener.GetContext();
    HttpListenerRequest request = context.get_Request();
    // This application sends a cookie to the client marking the time 
    // they visited. 
    Cookie timeStampCookie = new Cookie("VisitDate", 
        DateTime.get_Now().ToString());
    // Obtain a response object.
    HttpListenerResponse response = context.get_Response();
    // Add the cookie to the response.
    response.SetCookie(timeStampCookie);
    // Construct a response.
    String responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
    response.set_ContentEncoding(System.Text.Encoding.get_UTF8());
    ubyte buffer[] = System.Text.Encoding.get_UTF8().GetBytes(responseString);
    // Send the response.
    response.Close(buffer, true);
    // Closing the context closes the request.
    listener.Stop();
} //SimpleCookieExample

Windows 98, Windows Server 2003, Windows XP Media Center Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Show: