HttpListener.BeginGetContext(AsyncCallback, Object) HttpListener.BeginGetContext(AsyncCallback, Object) HttpListener.BeginGetContext(AsyncCallback, Object) HttpListener.BeginGetContext(AsyncCallback, Object) Method

定義

受信要求の非同期の取得を開始します。Begins asynchronously retrieving an incoming request.

public:
 IAsyncResult ^ BeginGetContext(AsyncCallback ^ callback, System::Object ^ state);
public IAsyncResult BeginGetContext (AsyncCallback callback, object state);
member this.BeginGetContext : AsyncCallback * obj -> IAsyncResult
Public Function BeginGetContext (callback As AsyncCallback, state As Object) As IAsyncResult

パラメーター

callback
AsyncCallback AsyncCallback AsyncCallback AsyncCallback

クライアント要求が使用可能なときに呼び出すメソッドを参照する AsyncCallback デリゲート。An AsyncCallback delegate that references the method to invoke when a client request is available.

state
Object Object Object Object

操作に関する情報を格納するユーザー定義のオブジェクト。A user-defined object that contains information about the operation. このオブジェクトは、操作の完了時に callback デリゲートに渡されます。This object is passed to the callback delegate when the operation completes.

戻り値

非同期操作のステータスを示す IAsyncResult オブジェクト。An IAsyncResult object that indicates the status of the asynchronous operation.

例外

Win32 関数呼び出しが失敗しました。A Win32 function call failed. 例外の ErrorCode プロパティを調べて、例外の原因を確認します。Check the exception's ErrorCode property to determine the cause of the exception.

このオブジェクトが開始されていないか、現在停止されています。This object has not been started or is currently stopped.

オブジェクトが閉じています。This object is closed.

次のコード例では、 BeginGetContextメソッドを使用して、受信クライアント要求を処理するコールバックメソッドを指定する方法を示します。The following code example demonstrates using the BeginGetContext method to specify a callback method that will handle incoming client requests.


public static void NonblockingListener(string [] prefixes)
{
    HttpListener listener = new HttpListener();
    foreach (string s in prefixes)
    {
        listener.Prefixes.Add(s);
    }
    listener.Start();
    IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback),listener);
    // Applications can do some work here while waiting for the 
    // request. If no work can be done until you have processed a request,
    // use a wait handle to prevent this thread from terminating
    // while the asynchronous operation completes.
    Console.WriteLine("Waiting for request to be processed asyncronously.");
    result.AsyncWaitHandle.WaitOne();
    Console.WriteLine("Request processed asyncronously.");
    listener.Close();
}
Public Shared Sub NonblockingListener(ByVal prefixes As String())
    Dim listener As HttpListener = New HttpListener()

    For Each s As String In prefixes
        listener.Prefixes.Add(s)
    Next

    listener.Start()
    Dim result As IAsyncResult = listener.BeginGetContext(New AsyncCallback(AddressOf ListenerCallback), listener)
    ' Applications can do some work here while waiting for the 
    ' request. If no work can be done until you have processed a request,
    ' use a wait handle to prevent this thread from terminating
    ' while the asynchronous operation completes.
    Console.WriteLine("Waiting for request to be processed asyncronously.")
    result.AsyncWaitHandle.WaitOne()
    Console.WriteLine("Request processed asyncronously.")
    listener.Close()
End Sub

次のコード例では、コールバックメソッドを実装します。The following code example implements a callback 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 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

注釈

メソッドBeginGetContextは、受信クライアント要求を受信するための非同期 (非ブロッキング) 呼び出しを開始します。The BeginGetContext method begins an asynchronous (non-blocking) call to receive incoming client requests. このメソッドを呼び出す前に、 Startメソッドを呼び出し、 Prefixesプロパティによって返されるに uri 文字列を追加することによって、 HttpListenerPrefixCollectionリッスンする Uniform Resource Identifier (uri) プレフィックスを少なくとも1つ追加する必要があります。Before calling this method, you must call the Start method and add at least one Uniform Resource Identifier (URI) prefix to listen for by adding the URI strings to the HttpListenerPrefixCollection returned by the Prefixes property.

非同期操作は、メソッドをEndGetContext呼び出すことによって完了する必要があります。The asynchronous operation must be completed by calling the EndGetContext method. 通常、メソッドはcallbackデリゲートによって呼び出されます。Typically, the method is invoked by the callback delegate.

このメソッドは、操作の完了時にブロックされません。This method does not block while the operation completes. 受信要求を取得し、操作が完了するまでブロックするにGetContextは、メソッドを呼び出します。To get an incoming request and block until the operation completes, call the GetContext method.

非同期プログラミングモデルの使用方法の詳細については、「同期メソッドの非同期呼び出し」を参照してください。For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously

注意 (呼び出し元)

このメンバーは、アプリケーションでネットワーク トレースが有効にされている場合にトレース情報を出力します。This member outputs trace information when you enable network tracing in your application. 詳細については、「 .NET Framework のネットワークトレース」を参照してください。For more information, see Network Tracing in the .NET Framework.

適用対象