HttpListenerResponse Class

Represents a response to a request being handled by an HttpListener object.

System.Object
  System.Net.HttpListenerResponse

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

'Declaration
Public NotInheritable Class HttpListenerResponse _
	Implements IDisposable

The HttpListenerResponse type exposes the following members.

  NameDescription
Public propertyContentEncodingGets or sets the Encoding for this response's OutputStream.
Public propertyContentLength64Gets or sets the number of bytes in the body data included in the response.
Public propertyContentTypeGets or sets the MIME type of the content returned.
Public propertyCookiesGets or sets the collection of cookies returned with the response.
Public propertyHeadersGets or sets the collection of header name/value pairs returned by the server.
Public propertyKeepAliveGets or sets a value indicating whether the server requests a persistent connection.
Public propertyOutputStreamGets a Stream object to which a response can be written.
Public propertyProtocolVersionGets or sets the HTTP version used for the response.
Public propertyRedirectLocationGets or sets the value of the HTTP Location header in this response.
Public propertySendChunkedGets or sets whether the response uses chunked transfer encoding.
Public propertyStatusCodeGets or sets the HTTP status code to be returned to the client.
Public propertyStatusDescriptionGets or sets a text description of the HTTP status code returned to the client.
Top

  NameDescription
Public methodAbortCloses the connection to the client without sending a response.
Public methodAddHeaderAdds the specified header and value to the HTTP headers for this response.
Public methodAppendCookieAdds the specified Cookie to the collection of cookies for this response.
Public methodAppendHeaderAppends a value to the specified HTTP header to be sent with this response.
Public methodCloseSends the response to the client and releases the resources held by this HttpListenerResponse instance.
Public methodClose(Byte(), Boolean)Returns the specified byte array to the client and releases the resources held by this HttpListenerResponse instance.
Public methodCopyFromCopies properties from the specified HttpListenerResponse to this response.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodRedirectConfigures the response to redirect the client to the specified URL.
Public methodSetCookieAdds or updates a Cookie in the collection of cookies sent with this response.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Explicit interface implemetationPrivate methodIDisposable.DisposeInfrastructure. Releases all resources used by the HttpListenerResponse.
Top

When a client makes a request for a resource handled by an HttpListener object, the request and response are made available to your application in an HttpListenerContext object. The request is represented by an HttpListenerRequest object and is available in the HttpListenerContext.Request property. The response is represented by an HttpListenerResponse object and is available in the HttpListenerContext.Response property.

You can customize the response by setting various properties, such as StatusCode, StatusDescription, and Cookies. Use the HttpListenerResponse.OutputStream property to obtain a Stream instance to which response data can be written. Finally, send the response data to the client by calling the Close method.

The following code example demonstrates responding to a client request.

// This example requires the System and System.Net namespaces. 
public static void SimpleListenerExample(string[] prefixes)
{
    if (!HttpListener.IsSupported)
    {
        Console.WriteLine ("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
        return;
    }
    // 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;
    System.IO.Stream output = response.OutputStream;
    output.Write(buffer,0,buffer.Length);
    // You must close the output stream.
    output.Close();
    listener.Stop();
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft