Export (0) Print
Expand All

HttpListenerResponse.Cookies Property

Gets or sets the collection of cookies returned with the response.

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

public CookieCollection Cookies { get; set; }

Property Value

Type: System.Net.CookieCollection
A CookieCollection that contains cookies to accompany the response. The collection is empty if no cookies have been added to the response.

A cookie is name/value text data from a Web server that is stored on the local (client) computer. The following cookie formats are supported: Netscape, RFC 2109, and RFC 2965. The Netscape cookie specification is available at http://wp.netscape.com/newsref/std/cookie_spec.html; the RFC documents are available at http://www.rfc-editor.org.

The following code example checks a request for a cookie, and returns a new cookie with the response if the request did not have one.

// This example requires the System and System.Net namespaces. 

public static string NextCustomerID()
{
    // A real-world application would do something more robust 
    // to ensure uniqueness. 
    return DateTime.Now.ToString();
}
public static void SimpleListenerCookieExample(string[] prefixes)
{
    // Create a listener.
    HttpListener listener = new HttpListener();
    // Add the prefixes. 
    foreach (string s in prefixes)
    {
        listener.Prefixes.Add(s);
    }
    listener.IgnoreWriteExceptions = true;
    listener.Start();
    Console.WriteLine("Listening...");
    // Note: The GetContext method blocks while waiting for a request. 
    HttpListenerContext context = listener.GetContext();
    HttpListenerRequest request = context.Request;
    string customerID = null;

    // Did the request come with a cookie?
    Cookie cookie = request.Cookies["ID"];
    if (cookie != null)
    {
         customerID=cookie.Value;
    }
    if (customerID !=null)
    {
          Console.WriteLine("Found the cookie!");
    }
    // Get the response object.
    HttpListenerResponse response = context.Response;
    // If they didn't provide a cookie containing their ID, give them one. 
    if (customerID == null)
    {
        customerID = NextCustomerID();
        Cookie cook = new Cookie("ID", customerID );
        response.AppendCookie (cook);
    }
    // Construct a response.
    string responseString = "<HTML><BODY> Hello " + customerID + "!</BODY></HTML>";
    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
    // Get the response stream and write the response to it.
    response.ContentLength64 = buffer.Length;
    System.IO.Stream output = response.OutputStream;
    output.Write(buffer,0,buffer.Length);
    // You must close the output stream.
    output.Close();
    // Closing the response sends the response to the client.
    response.Close();
    listener.Stop();
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft