HttpListenerResponse.SetCookie(Cookie) 메서드

정의

이 응답과 함께 보낸 쿠키의 컬렉션에서 Cookie를 추가하거나 업데이트합니다.Adds or updates a Cookie in the collection of cookies sent with this response.

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

매개 변수

cookie
Cookie

이 응답에 대한 Cookie입니다.A Cookie for this response.

예외

cookienull입니다.cookie is null.

쿠키가 컬렉션에 이미 있고 바꿀 수 없는 경우The cookie already exists in the collection and could not be replaced.

예제

다음 코드 예제에서는이 메서드를 호출 하는 방법을 보여 줍니다.The following code example demonstrates calling this method.

// 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

설명

Name, DomainPath 속성의 값이 같으면 두 쿠키는 동일 하 게 간주 됩니다.Two cookies are considered the same if the values of their Name, Domain, and Path properties are the same. 이러한 세 가지 정보를 동일한 경우 메서드는 쿠키를 업데이트 하려고 시도 합니다.If these three pieces of information are the same, the method attempts to update the cookie. 이름 및 도메인 비교는 대/소문자를 구분 하지 않지만 경로 비교는 대/소문자를 구분 합니다.The name and domain comparisons are not case sensitive, but the path comparison is case sensitive.

적용 대상

추가 정보