HttpListener.EndGetContext(IAsyncResult) Método

Definición

Finaliza una operación asincrónica para recuperar una solicitud de cliente de entrada.

public:
 System::Net::HttpListenerContext ^ EndGetContext(IAsyncResult ^ asyncResult);
public System.Net.HttpListenerContext EndGetContext (IAsyncResult asyncResult);
member this.EndGetContext : IAsyncResult -> System.Net.HttpListenerContext
Public Function EndGetContext (asyncResult As IAsyncResult) As HttpListenerContext

Parámetros

asyncResult
IAsyncResult

Un objeto IAsyncResult que se obtuvo al iniciar la operación asincrónica.

Devoluciones

Un objeto HttpListenerContext que representa la solicitud de cliente.

Excepciones

No se ha obtenido el valor asyncResult llamando al método BeginGetContext(AsyncCallback, Object).

asyncResult es null.

Se ha producido un error en una llamada de función de Win32. Examine la propiedad ErrorCode de la excepción para determinar su causa.

Ya se llamó al método EndGetContext(IAsyncResult) para el objeto asyncResult especificado.

Este objeto está cerrado.

Ejemplos

En el ejemplo de código siguiente se muestra la implementación de un método de devolución de llamada que llama al EndGetContext método .

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 Shared Sub ListenerCallback(ByVal result As IAsyncResult)
    Dim listener As HttpListener = CType(result.AsyncState, HttpListener)
    ' Call EndGetContext to complete the asynchronous operation.
    Dim context As HttpListenerContext = listener.EndGetContext(result)
    Dim request As HttpListenerRequest = context.Request
    ' Obtain a response object.
    Dim response As HttpListenerResponse = context.Response
    ' Construct a response.
    Dim responseString As String = "<HTML><BODY> Hello world!</BODY></HTML>"
    Dim buffer As Byte() = System.Text.Encoding.UTF8.GetBytes(responseString)
    ' Get a response stream and write the response to it.
    response.ContentLength64 = buffer.Length
    Dim output As System.IO.Stream = response.OutputStream
    output.Write(buffer, 0, buffer.Length)
    ' You must close the output stream.
    output.Close()
End Sub

Comentarios

Se EndGetContext llama al método , normalmente dentro de un método de devolución de llamada definido por la aplicación invocado por un delegado, para obtener el HttpListenerContext objeto que contiene una solicitud de cliente entrante y su respuesta asociada. Este método completa una operación iniciada anteriormente llamando al BeginGetContext método . Si la operación no se ha completado, este método se bloquea hasta que lo haga.

Dado que llamar al EndGetContext método requiere el HttpListener objeto , este objeto normalmente se pasa a un método de devolución de llamada mediante el objeto de estado pasado al BeginGetContext método . Puede obtener este objeto de estado mediante la AsyncState propiedad del asyncResult objeto .

Para obtener información detallada sobre el uso del modelo de programación asincrónica, vea Llamar a métodos sincrónicos de forma asincrónica.

Notas a los autores de las llamadas

Este miembro genera información de seguimiento cuando se habilita el seguimiento de red en la aplicación. Para obtener más información, vea Seguimiento de red en .NET Framework.

Se aplica a