HttpListener.EndGetContext(IAsyncResult) 方法

定义

完成检索传入的客户端请求的异步操作。

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

参数

asyncResult
IAsyncResult

启动异步操作时获得的 IAsyncResult 对象。

返回

表示客户端请求的 HttpListenerContext 对象。

例外

未通过调用 BeginGetContext(AsyncCallback, Object) 方法获取 asyncResult

asyncResultnull

Win32 函数调用失败。 检查异常的 ErrorCode 属性以确定导致异常的原因。

已为指定的 asyncResult 对象调用 EndGetContext(IAsyncResult) 方法。

此对象已关闭。

示例

下面的代码示例演示调用 方法的回调方法的 EndGetContext 实现。

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

注解

调用 EndGetContext 方法(通常在委托调用的应用程序定义的回调方法中)以获取 HttpListenerContext 包含传入客户端请求及其关联响应的对象。 此方法完成之前通过调用 BeginGetContext 方法启动的操作。 如果操作尚未完成,则此方法将阻止,直到它完成。

由于调用 EndGetContext 方法需要 HttpListener 对象,因此此对象通常通过使用传递到 方法中的状态对象传递到 BeginGetContext 回调方法中。 可以使用 对象的 AsyncState 属性 asyncResult 获取此状态对象。

有关使用异步编程模型的详细信息,请参阅 异步调用同步方法

调用方说明

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅.NET Framework中的网络跟踪

适用于