HttpListenerResponse.Close 메서드

정의

클라이언트에 응답을 보내고 이 HttpListenerResponse 인스턴스에서 보유한 리소스를 해제합니다.Sends the response to the client and releases the resources held by this HttpListenerResponse instance.

오버로드

Close()

클라이언트에 응답을 보내고 이 HttpListenerResponse 인스턴스에서 보유한 리소스를 해제합니다.Sends the response to the client and releases the resources held by this HttpListenerResponse instance.

Close(Byte[], Boolean)

지정된 바이트 배열을 클라이언트에 반환하고 이 HttpListenerResponse 인스턴스에서 보유한 리소스를 해제합니다.Returns the specified byte array to the client and releases the resources held by this HttpListenerResponse instance.

Close()

클라이언트에 응답을 보내고 이 HttpListenerResponse 인스턴스에서 보유한 리소스를 해제합니다.Sends the response to the client and releases the resources held by this HttpListenerResponse instance.

public:
 void Close();
public void Close ();
member this.Close : unit -> unit
Public Sub Close ()

예제

다음 코드 예제에서는이 메서드를 호출 하 여 클라이언트에 Forbidden (403) 응답을 보내는 방법을 보여 줍니다.The following code example demonstrates calling this method to send a Forbidden (403) response to the client.

static string message403;
static HttpListenerResponse preMade403Response;
static void SendBadCertificateResponse(HttpListenerResponse response)
{
    if (preMade403Response == null)
    {
        // Set up an authentication error response template.
        response.StatusCode = (int)HttpStatusCode.Forbidden;
        response.StatusDescription = "403 Forbidden";
        response.ProtocolVersion = new Version("1.1");
        response.SendChunked = false;

        preMade403Response = response;
    }
    else
    {
        response.CopyFrom(preMade403Response);
    }

    // The response body cannot be saved in the template.

    StringBuilder message = new StringBuilder();
    message.Append("<HTML><BODY>");
    message.Append("<p> Error message 403: Access is denied due to a missing or invalid client certificate.</p>");
    message.Append("</BODY></HTML>");
    message403 = message.ToString();

    // Turn the error message into a byte array using the 
    // encoding from the response when present.
    System.Text.Encoding encoding = response.ContentEncoding;
    if (encoding == null)
    {
        encoding = System.Text.Encoding.UTF8;
        response.ContentEncoding = encoding;
    }

    byte[] buffer = encoding.GetBytes(message403);
    response.ContentLength64 = buffer.Length;
    // Write the error message.
    System.IO.Stream stream = response.OutputStream;
    stream.Write(buffer, 0, buffer.Length);
    // Send the response.
    response.Close();
}
Private Shared message403 As String
Private Shared preMade403Response As HttpListenerResponse
Private Shared Sub SendBadCertificateResponse(ByVal response As HttpListenerResponse)
    
    If preMade403Response Is Nothing Then
        ' Set up an authentication error response template.
        response.StatusCode = Cint(HttpStatusCode.Forbidden)
        response.StatusDescription = "403 Forbidden"
        response.ProtocolVersion = New Version("1.1")
        response.SendChunked = False
    Else
        response.CopyFrom(preMade403Response) 
    End If
    
    ' The response body cannot be saved in the template.
    Dim message As New StringBuilder()
    message.Append("<HTML><BODY>")
    message.Append("<p> Error message 403: Access is denied due to a missing or invalid client certificate.</p>")
    message.Append("</BODY></HTML>")
    message403 = message.ToString()

    ' Turn the error message into a byte array using the 
    ' encoding from the response when present.
    Dim encoding As System.Text.Encoding = response.ContentEncoding
    If encoding Is Nothing Then
        encoding = System.Text.Encoding.UTF8
        response.ContentEncoding = encoding
    End If

    Dim buffer() As Byte = encoding.GetBytes(message403)
    response.ContentLength64 = buffer.Length
    ' Write the error message.
    Dim stream As System.IO.Stream = response.OutputStream
    stream.Write(buffer, 0, buffer.Length)
    ' Send the response.
    response.Close()
End Sub

설명

이 메서드는 응답과 연결 된 HttpListenerRequest 응답 스트림을 닫습니다.This method closes the response stream and the HttpListenerRequest associated with the response.

추가 정보

Close(Byte[], Boolean)

지정된 바이트 배열을 클라이언트에 반환하고 이 HttpListenerResponse 인스턴스에서 보유한 리소스를 해제합니다.Returns the specified byte array to the client and releases the resources held by this HttpListenerResponse instance.

public:
 void Close(cli::array <System::Byte> ^ responseEntity, bool willBlock);
public void Close (byte[] responseEntity, bool willBlock);
member this.Close : byte[] * bool -> unit
Public Sub Close (responseEntity As Byte(), willBlock As Boolean)

매개 변수

responseEntity
Byte[]

클라이언트에 보낼 응답이 들어 있는 Byte 배열입니다.A Byte array that contains the response to send to the client.

willBlock
Boolean

스트림을 클라이언트로 플러시하는 동안 실행을 차단하려면 true이고, 그렇지 않으면 false입니다.true to block execution while flushing the stream to the client; otherwise, false.

예외

responseEntitynull입니다.responseEntity is null.

이 개체가 닫혀 있는 경우This object is closed.

예제

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

// This example requires the System and System.Net namespaces.
public static void SimpleListenerExample2(string[] prefixes)
{
    // URI prefixes are required,
    // for example "http://contoso.com:8080/index/".
    if (prefixes == null || prefixes.Length == 0)
      throw new ArgumentException("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;
    // Obtain a response object.
    HttpListenerResponse response = context.Response;
    // Construct a response.
    string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
    // Get a response stream and write the response to it.
    response.ContentLength64 = buffer.Length;
    // Demonstrate using the close overload that takes an 
    // entity body.
    // Specify true to block while data is transmitted.
    response.Close(buffer, true);
    listener.Stop();
}
Public Shared Sub SimpleListenerExample2(ByVal prefixes As String())
    ' URI prefixes are required,
    ' for example "http://contoso.com:8080/index/".
    If prefixes Is Nothing Or prefixes.Length = 0 Then
        Throw New ArgumentException("prefixes")
    End If

    ' 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
    ' Obtain a response object.
    Dim response As HttpListenerResponse = context.Response
    ' Construct a response
    Dim responseString As String = "<HTML><BODY> Hello world!</BODY></HTML>"
    Dim buffer As Byte() = System.Text.Encoding.UTF8.GetBytes(responseString)
    ' Get a response stream and write the response to it.
    response.ContentLength64 = buffer.Length
    ' Demonstrate using the close overload that takes an 
    ' entity body.
    ' Specify true to block while data Is transmitted.
    response.Close(buffer, True)
    listener.Stop()
End Sub

설명

응답을 사용 하 여 본문 데이터를 전송 하는 경우 본문 데이터를 OutputStream 속성에 쓰고 Close 메서드를 호출 하는 대신이 메서드를 사용 하 여 Byte 배열로 보낼 수 있습니다.If you are sending body data with the response, you can use this method to send it as a Byte array instead of writing the body data to the OutputStream property and calling the Close method.

이 메서드는 응답과 연결 된 HttpListenerRequest 응답 스트림을 닫습니다.This method closes the response stream and the HttpListenerRequest associated with the response.

추가 정보

적용 대상