HttpListener.Start Metodo

Definizione

Consente a questa istanza di ricevere richieste in ingresso.

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

Eccezioni

Chiamata di funzione Win32 non riuscita. Controllare la proprietà dell'eccezione ErrorCode per determinare la causa dell'eccezione.

Questo oggetto è chiuso.

Esempio

Nell'esempio di codice seguente viene illustrato l'uso del metodo per avviare l'elaborazione Start delle richieste in ingresso.


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

Commenti

Questo metodo deve essere chiamato prima di chiamare il GetContext metodo o BeginGetContext .

Dopo aver avviato un HttpListener oggetto, è possibile usare il Stop metodo per arrestarlo.

Note

Se questa istanza del listener usa https, è necessario installare e selezionare un certificato server. In caso contrario, una HttpWebRequest query di questo HttpListener errore avrà esito negativo con una chiusura imprevista della connessione. È possibile configurare i certificati server e altre opzioni del listener usando HttpCfg.exe.

Note per i chiamanti

Questo membro restituisce informazioni di traccia quando si abilita la traccia di rete nell'applicazione. Per altre informazioni, vedere Network Tracing in the .NET Framework.

Si applica a