Ανάγνωση στα Αγγλικά Επεξεργασία

Κοινή χρήση μέσω


HttpListenerResponse.Close Method

Definition

Sends the response to the client and releases the resources held by this HttpListenerResponse instance.

Overloads

Close()

Sends the response to the client and releases the resources held by this HttpListenerResponse instance.

Close(Byte[], Boolean)

Returns the specified byte array to the client and releases the resources held by this HttpListenerResponse instance.

Close()

Source:
HttpListenerResponse.Managed.cs
Source:
HttpListenerResponse.Managed.cs
Source:
HttpListenerResponse.Managed.cs

Sends the response to the client and releases the resources held by this HttpListenerResponse instance.

[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public void Close ();
public void Close ();
Attributes

Examples

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();
}

Remarks

This method closes the response stream and the HttpListenerRequest associated with the response.

See also

Applies to

.NET 9 και άλλες εκδόσεις
Προϊόν Εκδόσεις
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Close(Byte[], Boolean)

Source:
HttpListenerResponse.Managed.cs
Source:
HttpListenerResponse.Managed.cs
Source:
HttpListenerResponse.Managed.cs

Returns the specified byte array to the client and releases the resources held by this HttpListenerResponse instance.

[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public void Close (byte[] responseEntity, bool willBlock);
public void Close (byte[] responseEntity, bool willBlock);

Parameters

responseEntity
Byte[]

A Byte array that contains the response to send to the client.

willBlock
Boolean

true to block execution while flushing the stream to the client; otherwise, false.

Attributes

Exceptions

responseEntity is null.

This object is closed.

Examples

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();
}

Remarks

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.

This method closes the response stream and the HttpListenerRequest associated with the response.

If false is specified as the willBlock, the method returns immediately and the connection is closed asynchronously once the data is sent.

Σημείωση

If closing asynchronously, it is not possible for the caller to determine when the data have been sent. In addition to that, disposing the HttpListenerResponse, HttpListenerContext or HttpListener may close the connection before all the data are sent.

See also

Applies to

.NET 9 και άλλες εκδόσεις
Προϊόν Εκδόσεις
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1