Cookies Property

HttpListenerResponse.Cookies Property

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

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

Public Property Cookies As CookieCollection

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; the RFC documents are available at

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 Shared Function NextCustomerID() As String
    ' A real-world application would do something more robust
    ' to ensure uniqueness.
    Return Date.Now.ToString()
End Function
Public Shared Sub SimpleListenerCookieExample(ByVal prefixes() As String)
    ' Create a listener.
    Dim listener As New HttpListener()
    ' Add the prefixes.
    For Each s As String In prefixes
    Next s
    listener.IgnoreWriteExceptions = True
    ' Note: The GetContext method blocks while waiting for a request. 
    Dim context As HttpListenerContext = listener.GetContext()
    Dim request As HttpListenerRequest = context.Request
    Dim customerID As String = Nothing

    ' Did the request come with a cookie?
    Dim cookie As Cookie = request.Cookies("ID")
    If cookie IsNot Nothing Then
        customerID = cookie.Value
    End If
    If customerID IsNot Nothing Then
        Console.WriteLine("Found the cookie!")
    End If
    ' Get the response object.
    Dim response As HttpListenerResponse = context.Response
    ' If they didn't provide a cookie containing their ID, give them one.
    If customerID Is Nothing Then
        customerID = NextCustomerID()
        Dim cook As New Cookie("ID", customerID)
    End If
    ' Construct a response.
    Dim responseString As String = "<HTML><BODY> Hello " & customerID & "!</BODY></HTML>"
    Dim buffer() As Byte = System.Text.Encoding.UTF8.GetBytes(responseString)
    ' Get the response stream and write the response to it.
    response.ContentLength64 = buffer.Length
    Dim output As System.IO.Stream = response.OutputStream
    output.Write(buffer, 0, buffer.Length)
    ' You must close the output stream.
    ' Closing the response sends the response to the client.
End Sub

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Community Additions

© 2016 Microsoft