MSDN Documentation

HttpListenerResponse Class

Represents the response that will be sent back to an HttpListener client.

public sealed class HttpListenerResponse : IDisposable

The HttpListenerResponse class provides the necessary properties and methods to construct and send an HTTP response back to a client that has connected to an HttpListener.

When a client sends an HTTP request to an HttpListener, the listener accepts the request and provides an HttpListenerContext object. This context object contains both the incoming request (HttpListenerRequest) and the outgoing response (HttpListenerResponse).

Syntax

public sealed class HttpListenerResponse : IDisposable

Members

Properties

[property] long ContentLength64 { get; set; }
Gets or sets the value of the Content-Length header. If this property is not set, the value is inferred from the content sent.
[property] string ContentType { get; set; }
Gets or sets the value of the Content-Type header. This property specifies the type of data in the response body.
[property] System.Net.CookieCollection Cookies { get; }
Gets a collection of cookies that are to be sent with the response.
[property] System.Net.Headers.WebHeaderCollection Headers { get; }
Gets a collection of all the HTTP headers to be sent with the response.
[property] string Location { get; set; }
Gets or sets the value of the Location header. This header is used to redirect the client to a different URL.
[property] int ProtocolVersion { get; set; }
Gets or sets the HTTP version of the response.
[property] System.Net.StatusCode StatusCode { get; set; }
Gets or sets the HTTP status code to be sent back to the client.
[property] string StatusDescription { get; set; }
Gets or sets the descriptive text to accompany the HTTP status code.
[property] bool SendChunked { get; set; }
Gets or sets a value that indicates whether to send the response using chunked transfer encoding. This property should be set before the response is sent.

Methods

Remarks

To send a response, you typically:

  1. Set the StatusCode property.
  2. Set the ContentType if you are sending a specific content type (e.g., text/html, application/json).
  3. Set the ContentLength64 property if you know the exact size of the response body beforehand. If not set, the server may use chunked encoding.
  4. Get the OutputStream from the HttpListenerResponse object.
  5. Write the content of your response to the OutputStream.
  6. Call the Close() method to finish sending the response and close the connection.

It is important to call Close() to ensure that the response is properly sent to the client and the resources are released.

Examples

Returning plain text


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

public class SimpleServer
{
    public static void Main(string[] args)
    {
        HttpListener listener = new HttpListener();
        listener.Prefixes.Add("http://localhost:8080/");
        listener.Start();
        Console.WriteLine("Listening on http://localhost:8080/");

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

            string responseString = "Hello from HttpListenerResponse!";
            byte[] buffer = Encoding.UTF8.GetBytes(responseString);

            response.ContentLength64 = buffer.Length;
            response.ContentType = "text/plain";
            response.OutputStream.Write(buffer, 0, buffer.Length);
            response.Close(); // Sends the response
        }
    }
}
            

Returning HTML


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

public class HtmlServer
{
    public static void Main(string[] args)
    {
        HttpListener listener = new HttpListener();
        listener.Prefixes.Add("http://localhost:8081/");
        listener.Start();
        Console.WriteLine("Listening on http://localhost:8081/");

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

            string html = @"
            
            
            
                HTML Response
                
            
            
                

This is an HTML response!

Sent via HttpListenerResponse.

"; byte[] buffer = Encoding.UTF8.GetBytes(html); response.ContentLength64 = buffer.Length; response.ContentType = "text/html"; response.OutputStream.Write(buffer, 0, buffer.Length); response.Close(); } } }

See Also