HttpListener::Close Method ()


Shuts down the HttpListener.

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

void Close()

After calling this method, you can no longer use the HttpListener object. To temporarily pause an HttpListener object, use the Stop method.

This method shut downs the HttpListener object without processing queued requests. Any pending requests are unable to complete.

Notes to Callers:

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

The following code example demonstrates calling this method.

// 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.");
    // URI prefixes are required,
    // for example "".
    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)
    // 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;
    // You must close the output stream.

.NET Framework
Available since 2.0
Return to top
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft