HttpListener.EndGetContext(IAsyncResult) HttpListener.EndGetContext(IAsyncResult) HttpListener.EndGetContext(IAsyncResult) HttpListener.EndGetContext(IAsyncResult) Method

定義

完成擷取傳入之用戶端要求的非同步作業。Completes an asynchronous operation to retrieve an incoming client request.

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 IAsyncResult IAsyncResult

在啟動非同步作業時取得的 IAsyncResult 物件。An IAsyncResult object that was obtained when the asynchronous operation was started.

傳回

HttpListenerContext 物件,表示用戶端要求。An HttpListenerContext object that represents the client request.

例外狀況

未呼叫 BeginGetContext(AsyncCallback, Object) 方法,取得 asyncResultasyncResult was not obtained by calling the BeginGetContext(AsyncCallback, Object) method.

Win32 函式呼叫失敗。A Win32 function call failed. 請檢查例外狀況的 ErrorCode 屬性,以判斷造成例外狀況的原因。Check the exception's ErrorCode property to determine the cause of the exception.

已針對指定的 asyncResult 物件,呼叫 EndGetContext(IAsyncResult) 方法。The EndGetContext(IAsyncResult) method was already called for the specified asyncResult object.

範例

下列程式碼範例示範呼叫EndGetContext方法的回呼方法的執行。The following code example shows the implementation of a callback method that calls the EndGetContext 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

備註

方法EndGetContext通常是在由委派所叫用之應用程式定義的回呼方法內呼叫, 以HttpListenerContext取得包含傳入用戶端要求及其相關回應的物件。The EndGetContext method is called, usually within an application-defined callback method invoked by a delegate, to obtain the HttpListenerContext object that contains an incoming client request and its associated response. 這個方法會藉由呼叫BeginGetContext方法來完成先前啟動的作業。This method completes an operation previously started by calling the BeginGetContext method. 如果作業尚未完成, 這個方法會封鎖, 直到執行為止。If the operation has not completed, this method blocks until it does.

因為呼叫EndGetContext方法HttpListener需要物件, 所以這個物件通常會使用傳入BeginGetContext方法的狀態物件傳遞至回呼方法。Because calling the EndGetContext method requires the HttpListener object, this object is typically passed into a callback method by using the state object passed into the BeginGetContext method. 您可以使用AsyncState asyncResult物件的屬性來取得這個狀態物件。You can obtain this state object by using the AsyncState property of the asyncResult object.

如需使用非同步程式設計模型的詳細資訊, 請參閱以非同步方式呼叫同步方法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.

適用於