HttpListener.Start 方法

定義

允許這個執行個體接收傳入要求。

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

例外狀況

Win32 函式呼叫失敗。 請檢查例外狀況的 ErrorCode 屬性,以判斷造成例外狀況的原因。

這個物件已經關閉。

範例

下列程式碼範例示範如何使用 Start 方法來開始處理傳入要求。


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

備註

呼叫 或 BeginGetContext 方法之前,必須先呼叫 GetContext 這個方法。

啟動 HttpListener 物件之後,您可以使用 Stop 方法來停止它。

注意

如果這個接聽程式實例使用 HTTPs,您必須安裝並選取伺服器憑證。 否則, HttpWebRequest 這個 HttpListener 的查詢將會失敗,且連線意外關閉。 您可以使用 HttpCfg.exe來設定伺服器憑證和其他接聽程式選項。

給呼叫者的注意事項

在應用程式中啟用網路追蹤時,這個成員會輸出追蹤資訊。 如需詳細資訊,請參閱.NET Framework中的網路追蹤

適用於