共用方式為


HttpListenerResponse.Close 方法

定義

將回應傳送至用戶端,並釋放這個 HttpListenerResponse 執行個體所持有的資源。

多載

Close()

將回應傳送至用戶端,並釋放這個 HttpListenerResponse 執行個體所持有的資源。

Close(Byte[], Boolean)

將指定的位元組陣列傳回至用戶端,並釋放這個 HttpListenerResponse 執行個體所持有的資源。

Close()

來源:
HttpListenerResponse.Managed.cs
來源:
HttpListenerResponse.Managed.cs
來源:
HttpListenerResponse.Managed.cs

將回應傳送至用戶端,並釋放這個 HttpListenerResponse 執行個體所持有的資源。

public:
 void Close();
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public void Close ();
public void Close ();
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.Close : unit -> unit
member this.Close : unit -> unit
Public Sub Close ()
屬性

範例

下列程式代碼範例示範如何呼叫這個方法,將 (403) 回應傳送 Forbidden 給用戶端。

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 與回應相關聯的 。

另請參閱

適用於

Close(Byte[], Boolean)

來源:
HttpListenerResponse.Managed.cs
來源:
HttpListenerResponse.Managed.cs
來源:
HttpListenerResponse.Managed.cs

將指定的位元組陣列傳回至用戶端,並釋放這個 HttpListenerResponse 執行個體所持有的資源。

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

參數

responseEntity
Byte[]

Byte 陣列,包含要傳送至用戶端的回應。

willBlock
Boolean

true 表示在清除資料流到用戶端時封鎖執行,否則為 false

屬性

例外狀況

responseEntitynull

這個物件已經關閉。

範例

下列程式代碼範例示範如何呼叫此方法。

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

備註

如果您要使用回應傳送本文數據,您可以使用這個方法將它當做 Byte 陣列傳送,而不是將本文數據 OutputStream 寫入屬性並呼叫 Close 方法。

這個方法會關閉回應資料流和 HttpListenerRequest 與回應相關聯的 。

如果 false 指定為 willBlock,則方法會立即傳回 ,而且一旦傳送數據,就會以異步方式關閉連接。

注意

如果以異步方式關閉,則呼叫端無法判斷何時傳送數據。 此外, HttpListenerResponse處置 或 HttpListenerContextHttpListener 可能會在傳送所有數據之前關閉連線。

另請參閱

適用於