HttpListener.GetContext 方法


等待傳入的要求,並在收到要求時傳回。Waits for an incoming request and returns when one is received.

 System::Net::HttpListenerContext ^ GetContext();
public System.Net.HttpListenerContext GetContext ();
member this.GetContext : unit -> System.Net.HttpListenerContext
Public Function GetContext () As HttpListenerContext


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


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.

-或--or- HttpListener 沒有任何要回應的統一資源識別元 (URI) 前置詞。The HttpListener does not have any Uniform Resource Identifier (URI) prefixes to respond to.

這個物件已經關閉。This object is closed.


下列程式碼範例將示範如何呼叫這個方法。The following code example demonstrates calling this method.

// This example requires the System and System.Net namespaces.
public static void SimpleListenerExample(string[] prefixes)
    if (!HttpListener.IsSupported)
        Console.WriteLine ("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
    // URI prefixes are required,
    // for example "".
    if (prefixes == null || prefixes.Length == 0)
      throw new ArgumentException("prefixes");
    // Create a listener.
    HttpListener listener = new HttpListener();
    // Add the prefixes.
    foreach (string s in prefixes)
    // Note: The GetContext method blocks while waiting for a request. 
    HttpListenerContext context = listener.GetContext();
    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;
    // You must close the output stream.
Public Shared Sub SimpleListenerExample(prefixes As String())
    If Not HttpListener.IsSupported Then
        Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.")
    End If
    ' URI prefixes are required,
    ' for example "".
    If prefixes Is Nothing Or prefixes.Length = 0 Then
        Throw New ArgumentException("prefixes")
    End If

    ' Create a listener
    Dim listener = New HttpListener()

    For Each s As String In prefixes
    ' Note: The GetContext method blocks while waiting for a request.
    Dim context As HttpListenerContext = listener.GetContext()
    ' Obtain a response object
    Dim request As HttpListenerRequest = context.Request
    ' Construct a response.
    Dim response As HttpListenerResponse = context.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.
End Sub


在呼叫這個方法之前,您必須先呼叫 Start 方法,並新增至少一個 URI 前置詞以接聽,方法是將 URI 字串新增至 Prefixes 屬性所傳回的 HttpListenerPrefixCollectionBefore calling this method, you must call the Start method and add at least one URI prefix to listen for by adding the URI strings to the HttpListenerPrefixCollection returned by the Prefixes property. 如需前置詞的詳細描述,請參閱 HttpListener 類別的總覽。For a detailed description of prefixes, see the HttpListener class overview.

這個方法會在等候傳入要求時封鎖。This method blocks while waiting for an incoming request. 如果您想要以非同步方式處理傳入要求(在不同的執行緒上),讓您的應用程式不會封鎖,請使用 BeginGetContext 方法。If you want incoming requests to be processed asynchronously (on separate threads) so that your application does not block, use the BeginGetContext method.


在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。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.