HttpListenerResponse Class

Namespace: System.Net.Http
Assembly: System (in System.dll)

Represents a response that is sent to an HttpListener client.

Remarks

When an HttpListener object receives an incoming request, it creates an HttpListenerContext object that contains the request and a corresponding HttpListenerResponse object.

You use the HttpListenerResponse object to construct and send the response to the client. The most common properties to set are:

After setting the appropriate properties and writing the response body, you must call the Close() method or use the using statement to send the response to the client.

Syntax

public sealed class HttpListenerResponse

Members

Properties

Properties

Name Description
Close() Sends the response to the client.
ContentEncoding Gets or sets the content encoding for the response.
ContentLength64 Gets or sets the number of bytes in the response body.
ContentType Gets or sets the content type of the response.
Cookies Gets or sets the cookies to be sent to the client.
Headers Gets a collection of headers to be sent with the response.
IsClientConnected Gets a value indicating whether the client is still connected.
KeepAlive Gets or sets a value indicating whether to keep the connection alive.
OutputStream Gets a stream to which you can write the response body.
ProtocolVersion Gets or sets the HTTP version of the response.
RedirectLocation Gets or sets the URL to which the client should be redirected.
ResponseUri Gets or sets the URI that identifies the resource that the response is intended for.
StatusCode Gets or sets the HTTP status code for the response.
StatusDescription Gets or sets the description of the HTTP status code.
enviados Gets a value indicating whether the response has been sent.

Methods

Methods

Name Description
AppendHeader(string, string) Appends a response header to the headers collection.
Close() Sends the response to the client and releases resources.
Close(byte[], bool) Sends the specified response body to the client and closes the connection.
Close(bool) Sends the response to the client and optionally closes the connection.
SetCookie(Cookie) Adds a cookie to the response.

Example

The following example demonstrates how to create a simple web server using HttpListener and send an HTML response.

using System;
using System.Net;
using System.Text;

public class SimpleHttpServer
{
    public static void Main(string[] args)
    {
        if (!HttpListener.IsSupported)
        {
            Console.WriteLine("HttpListener is not supported on this platform.");
            return;
        }

        HttpListener listener = new HttpListener();
        listener.Prefixes.Add("http://localhost:8080/");
        listener.Start();
        Console.WriteLine("Listening on http://localhost:8080/");

        while (true)
        {
            try
            {
                HttpListenerContext context = listener.GetContext();
                HttpListenerRequest request = context.Request;
                HttpListenerResponse response = context.Response;

                Console.WriteLine($"Received request for: {request.Url}");

                string responseString = @"
                    <!DOCTYPE html>
                    <html lang=""en"">
                    <head>
                        <meta charset=""UTF-8"">
                        <meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
                        <title>Hello World</title>
                        <style>
                            body { font-family: sans-serif; background-color: #f0f0f0; color: #333; text-align: center; padding-top: 50px; }
                            h1 { color: #0078d4; }
                        </style>
                    </head>
                    <body>
                        <h1>Hello from HttpListener!</h1>
                        <p>This is a response from your simple C# web server.</p>
                    </body>
                    </html>";

                byte[] buffer = Encoding.UTF8.GetBytes(responseString);
                response.ContentLength64 = buffer.Length;
                response.ContentType = "text/html";
                response.StatusCode = (int)HttpStatusCode.OK;

                using (System.IO.Stream output = response.OutputStream)
                {
                    output.Write(buffer, 0, buffer.Length);
                }
                response.Close();
                Console.WriteLine("Response sent.");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }
}