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 オブジェクト。

例外

asyncResultBeginGetContext(AsyncCallback, Object) メソッドの呼び出しで取得できませんでした。

asyncResultnullです。

Win32 関数呼び出しが失敗しました。 例外の ErrorCode プロパティを調べて、例外の原因を確認します。

EndGetContext(IAsyncResult) メソッドは、既に指定された asyncResult オブジェクトに対して呼び出されています。

オブジェクトが閉じています。

次のコード例は、 メソッドを呼び出すコールバック メソッドの実装を 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 渡されます。 この状態オブジェクトは、 オブジェクトの asyncResult プロパティをAsyncState使用して取得できます。

非同期プログラミング モデルの使用の詳細については、「同期メソッドの非同期呼び出し」を参照してください。

注意 (呼び出し元)

このメンバーは、アプリケーションでネットワーク トレースが有効にされている場合にトレース情報を出力します。 詳細については、「.NET Frameworkのネットワーク トレース」を参照してください。

適用対象