HttpListenerResponse.SetCookie(Cookie) Método

Definición

Agrega o actualiza un objeto Cookie en la colección de cookies enviada con esta respuesta.

public:
 void SetCookie(System::Net::Cookie ^ cookie);
public void SetCookie (System.Net.Cookie cookie);
member this.SetCookie : System.Net.Cookie -> unit
Public Sub SetCookie (cookie As Cookie)

Parámetros

cookie
Cookie

Un objeto Cookie para esta respuesta.

Excepciones

cookie es null.

La cookie ya existe en la colección y no se pudo reemplazar.

Ejemplos

En el ejemplo de código siguiente se muestra cómo llamar a este método.

// 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 Shared Sub SimpleCookieExample(ByVal prefixes As String())
    ' Create a listener
    Dim listener As HttpListener = New HttpListener()
    ' Add the prefixes
    For Each s As String In prefixes
        listener.Prefixes.Add(s)
    Next

    listener.Start()
    Console.WriteLine("Listening...")
    ' Note: The GetContext method blocks while waiting for a request.
    Dim context As HttpListenerContext = listener.GetContext()
    Dim request As HttpListenerRequest = context.Request

    ' This application sends a cookie to the client marking the time
    ' they visited.
    Dim timeStampCookie As Cookie = New Cookie("VisitDate", DateTime.Now.ToString())
    ' Obtain a response object.
    Dim response As HttpListenerResponse = context.Response
    ' Add the cookie to the response.
    response.SetCookie(timeStampCookie)
    ' Construct a response.
    Dim responseString As String = "<HTML><BODY> Hello world!</BODY></HTML>"
    response.ContentEncoding = System.Text.Encoding.UTF8
    Dim buffer As Byte() = System.Text.Encoding.UTF8.GetBytes(responseString)
    ' Send the response.
    response.Close(buffer, True)
    listener.Stop()
End Sub

Comentarios

Dos cookies se consideran iguales si los valores de sus Namepropiedades , Domainy Path son iguales. Si estos tres fragmentos de información son los mismos, el método intenta actualizar la cookie. Las comparaciones de nombres y dominios no distinguen mayúsculas de minúsculas, pero la comparación de rutas de acceso distingue mayúsculas de minúsculas.

Se aplica a

Consulte también