Export (0) Print
Expand All

HttpListenerResponse.AppendCookie Method

Adds the specified Cookie to the collection of cookies for this response.

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

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

Parameters

cookie

The Cookieto add to the collection to be sent with this response

Exception typeCondition

ArgumentNullException

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

Calling this method is equivalent to calling the Add method on the collection returned by the Cookies property.

If the specified cookie does not exist in the Cookies property's collection, cookie is added. If the cookie exists in the collection, cookie replaces it.

The following code example demonstrates adding a cookie to a response

// 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();
}

// 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.get_Now().ToString();
} //NextCustomerID

public static void SimpleListenerCookieExample(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.set_IgnoreWriteExceptions(true);
    listener.Start();
    Console.WriteLine("Listening...");
    // Note: The GetContext method blocks while waiting for a request. 
    HttpListenerContext context = listener.GetContext();
    HttpListenerRequest request = context.get_Request();
    String customerID = null;
    // Did the request come with a cookie?
    Cookie cookie = request.get_Cookies().get_Item("ID");
    if (cookie != null) {
        customerID = cookie.get_Value();
    }
    if (customerID != null) {
        Console.WriteLine("Found the cookie!");
    }
    // Get the response object.
    HttpListenerResponse response = context.get_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>";
    ubyte buffer[] = System.Text.Encoding.get_UTF8().
          GetBytes(responseString);
    // Get the response stream and write the response to it.
    response.set_ContentLength64(buffer.get_Length());
    System.IO.Stream output = response.get_OutputStream();
    output.Write(buffer, 0, buffer.get_Length());
    // You must close the output stream.
    output.Close();
    // Closing the response sends the response to the client.
    response.Close();
    listener.Stop();
} //SimpleListenerCookieExample

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

Community Additions

ADD
Show:
© 2014 Microsoft