다음을 통해 공유


HttpListenerResponse.Cookies 속성

정의

응답과 함께 반환된 쿠키의 컬렉션을 가져오거나 설정합니다.

public:
 property System::Net::CookieCollection ^ Cookies { System::Net::CookieCollection ^ get(); void set(System::Net::CookieCollection ^ value); };
public System.Net.CookieCollection Cookies { get; set; }
member this.Cookies : System.Net.CookieCollection with get, set
Public Property Cookies As CookieCollection

속성 값

응답과 함께 전송된 쿠키가 들어 있는 CookieCollection입니다. 응답에 쿠키가 추가되지 않았으면 이 컬렉션은 비어 있습니다.

예제

다음 코드 예제에서는 쿠키에 대한 요청을 확인하고 요청에 쿠키가 없는 경우 응답과 함께 새 쿠키를 반환합니다.

// 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 Shared Function NextCustomerID() As String
    ' A real-world application would do something more robust
    ' to ensure uniqueness.
    Return DateTime.Now.ToString()
End Function

Public Shared Sub SimpleListenerCookieExample(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.IgnoreWriteExceptions = True
    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
    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 Cookie = New Cookie("ID", customerID)
        response.AppendCookie(cook)
    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.
    output.Close()
    response.Close()
    listener.Stop()
End Sub

설명

쿠키는 로컬(클라이언트) 컴퓨터에 저장된 웹 서버의 이름/값 텍스트 데이터입니다. 지원되는 쿠키 형식은 Netscape, RFC 2109RFC 2965입니다.

적용 대상

추가 정보