HttpListener.GetContext HttpListener.GetContext HttpListener.GetContext HttpListener.GetContext Method

定義

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

public:
 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.

範例

下列程式碼範例將示範如何呼叫這個方法。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.");
        return;
    }
    // URI prefixes are required,
    // for example "http://contoso.com:8080/index/".
    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)
    {
        listener.Prefixes.Add(s);
    }
    listener.Start();
    Console.WriteLine("Listening...");
    // 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;
    output.Write(buffer,0,buffer.Length);
    // You must close the output stream.
    output.Close();
    listener.Stop();
}
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.")
        Return
    End If
    ' URI prefixes are required,
    ' for example "http://contoso.com:8080/index/".
    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
        listener.Prefixes.Add(s)
    Next
    listener.Start()
    Console.WriteLine("Listening...")
    ' Note: The GetContext method blocks while waiting for a request.
    Dim context As HttpListenerContext = listener.GetContext()
    Console.WriteLine("Listening...")
    ' 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.
    output.Close()
    listener.Stop()
End Sub

備註

在呼叫這個方法之前, 您必須呼叫Start方法, 並新增至少一個 uri 前置詞以接聽, 方法是將 URI 字串加入HttpListenerPrefixCollectionPrefixes屬性所傳回的。Before 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.

適用於