Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

HttpListener.EndGetContext Method

Completes an asynchronous operation to retrieve an incoming client request.

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

public:
HttpListenerContext^ EndGetContext (
	IAsyncResult^ asyncResult
)
public HttpListenerContext EndGetContext (
	IAsyncResult asyncResult
)
public function EndGetContext (
	asyncResult : IAsyncResult
) : HttpListenerContext
Not applicable.

Parameters

asyncResult

An IAsyncResult object that was obtained when the asynchronous operation was started.

Return Value

An HttpListenerContext object that represents the client request.
Exception typeCondition

ArgumentException

asyncResult was not obtained by calling the BeginGetContext method.

ArgumentNullException

asyncResult is a null reference (Nothing in Visual Basic).

InvalidOperationException

The EndGetContext method was already called for the specified asyncResult object.

ObjectDisposedException

This object is closed.

The EndGetContext method is called, usually within an application-defined callback method invoked by a delegate, to obtain the HttpListenerContext object that contains an incoming client request and its associated response. This method completes an operation previously started by calling the BeginGetContext method. If the operation has not completed, this method blocks until it does.

Because calling the EndGetContext method requires the HttpListener object, this object is typically passed into a callback method by using the state object passed into the BeginGetContext method. You can obtain this state object by using the AsyncState property of the asyncResult object.

For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously

Notes to Callers: This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing.

The following code example shows the implementation of a callback method that calls the EndGetContext method.

public static void ListenerCallback(IAsyncResult result)
{
    HttpListener listener = (HttpListener) result.AsyncState;
    // Call EndGetContext to complete the asynchronous operation.
    HttpListenerContext context = listener.EndGetContext(result);
    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();
}

public static void ListenerCallback(IAsyncResult result)
{
    HttpListener listener = (HttpListener)(result.get_AsyncState());
    // Call EndGetContext to complete the asynchronous operation.
    HttpListenerContext context = listener.EndGetContext(result);
    HttpListenerRequest request = context.get_Request();
    // Obtain a response object.
    HttpListenerResponse response = context.get_Response();
    // Construct a response.
    String responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
    ubyte buffer[] = System.Text.Encoding.get_UTF8().GetBytes(responseString);
    // Get a response stream and write the response to it.
    response.set_ContentLength64(buffer.get_Length());
    System.IO.Stream output = response.get_OutputStream();
    output.Write(buffer, 0, buffer.get_Length());
    // You must close the output stream.
    output.Close();
} //ListenerCallback

Windows 98, Windows Server 2003, Windows XP Media Center Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.