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.

範例

下列程式碼範例將示範如何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方法, 並新增至少一個統一資源識別元 (uri) 前置詞以接聽, 方法是將 URI 字串HttpListenerPrefixCollection加入至Prefixes屬性所傳回的。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.

適用於