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
-
Close()
Closes the response stream and signals to the
HttpListener
that the response is complete. -
Close(byte[] responseEntity, bool persistent)
Closes the response stream and sends the specified byte array as the response body. The
persistent
parameter indicates whether the connection should remain open for subsequent requests. -
CloseWithHeaders(byte[] responseEntity, bool persistent)
Closes the response stream, sends the specified byte array as the response body, and includes any previously set headers.
-
Dispose()
Releases all resources used by the
HttpListenerResponse
object. -
OutputStream.Write(byte[] buffer, int offset, int count)
Writes the specified number of bytes from the specified byte array to the response stream. This is the primary method for sending the response body.
-
Redirect(string url)
Sends an HTTP redirect to the client, instructing them to navigate to the specified URL.
Remarks
To send a response, you typically:
- Set the
StatusCode
property. - Set the
ContentType
if you are sending a specific content type (e.g.,text/html
,application/json
). - Set the
ContentLength64
property if you know the exact size of the response body beforehand. If not set, the server may use chunked encoding. - Get the
OutputStream
from theHttpListenerResponse
object. - Write the content of your response to the
OutputStream
. - 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();
}
}
}