共用方式為


HttpListener.BeginGetContext(AsyncCallback, Object) 方法

定義

開始非同步擷取傳入要求。

public:
 IAsyncResult ^ BeginGetContext(AsyncCallback ^ callback, System::Object ^ state);
public IAsyncResult BeginGetContext (AsyncCallback? callback, 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 委派,有用戶端要求時,它會參考要叫用的方法。

state
Object

使用者定義物件,包含作業的相關資訊。 作業完成時會將這個物件傳遞給 callback 委派。

傳回

IAsyncResult 物件,表示非同步作業的狀態。

例外狀況

Win32 函式呼叫失敗。 請檢查例外狀況的 ErrorCode 屬性,以判斷造成例外狀況的原因。

這個物件尚未啟動或目前已停止。

這個物件已經關閉。

範例

下列程式代碼範例示範如何使用 BeginGetContext 方法來指定將處理傳入用戶端要求的回呼方法。


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

下列程式代碼範例會實作回呼方法。

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 會開始異步 (非封鎖) 呼叫來接收傳入用戶端要求。 呼叫此方法之前,您必須呼叫 Start 方法,並將至少一個統一資源識別元 (URI) 前置詞新增至 HttpListenerPrefixCollection 屬性所傳回的 ,以接聽。Prefixes

異步操作必須藉由呼叫 EndGetContext 方法來完成。 一般而言,委派會叫 callback 用 方法。

此方法不會在作業完成時封鎖。 若要取得傳入要求並封鎖直到作業完成為止,請呼叫 GetContext 方法。

如需使用異步程序設計模型的詳細資訊,請參閱 異步呼叫同步方法

給呼叫者的注意事項

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱 .NET Framework 中的網路追蹤

適用於