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呼び出す必要があります。

オブジェクトを開始したら、 メソッドをHttpListenerStop使用して停止できます。

注意

このリスナー インスタンスで https を使用する場合は、サーバー証明書をインストールして選択する必要があります。 それ以外の場合、接続が予期せず閉じると、 HttpWebRequest この HttpListener クエリは失敗します。 HttpCfg.exe を使用 して、サーバー証明書とその他のリスナー オプションを構成できます。

注意 (呼び出し元)

このメンバーは、アプリケーションでネットワーク トレースが有効にされている場合にトレース情報を出力します。 詳細については、「.NET Frameworkのネットワーク トレース」を参照してください。

適用対象