HttpListener.Start 方法

定義

允許這個執行個體接收傳入要求。Allows this instance to receive incoming requests.

public:
 void Start();
public void Start ();
member this.Start : unit -> unit
Public Sub Start ()

例外狀況

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

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

範例

下列程式碼範例將示範如何使用 Start 方法開始處理傳入的要求。The following code example demonstrates using the Start method to begin processing incoming 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

備註

呼叫 GetContextBeginGetContext 方法之前,必須先呼叫這個方法。This method must be called before you call the GetContext or BeginGetContext method.

啟動 HttpListener 物件之後,您可以使用 Stop 方法來停止它。After you have started an HttpListener object, you can use the Stop method to stop it.

注意

如果此接聽程式實例使用 HTTPs,您必須安裝並選取伺服器憑證。If this listener instance uses https, you must install and select a Server Certificate. 否則,此 HttpListenerHttpWebRequest 查詢將會失敗,並出現非預期的連接關閉。Otherwise, an HttpWebRequest query of this HttpListener will fail with an unexpected close of the connection. 您可以使用 Httpcfg.exe 來設定伺服器憑證和其他接聽程式選項。You can configure Server Certificates and other listener options by using HttpCfg.exe. 如需詳細資訊,請參閱https://msdn.microsoft.com/library/default.asp?url=/library/http/http/httpcfg_exe.aspSee https://msdn.microsoft.com/library/default.asp?url=/library/http/http/httpcfg_exe.asp for more details.

給呼叫者的注意事項

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

適用於