HttpListener.Start Método

Definição

Permite que essa instância receba solicitações de entrada.

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

Exceções

Falha em uma chamada de função do Win32. Verifique a propriedade ErrorCode da exceção para determinar a causa da exceção.

Este objeto está fechado.

Exemplos

O exemplo de código a seguir demonstra como usar o Start método para começar a processar solicitações de entrada.


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

Comentários

Esse método deve ser chamado antes de chamar o GetContext método ou BeginGetContext .

Depois de iniciar um HttpListener objeto, você pode usar o Stop método para interrompê-lo.

Observação

Se essa instância de ouvinte usar https, você deverá instalar e selecionar um Certificado do Servidor. Caso contrário, uma HttpWebRequest consulta disso HttpListener falhará com um fechamento inesperado da conexão. Você pode configurar certificados de servidor e outras opções de ouvinte usando HttpCfg.exe.

Notas aos Chamadores

Esse membro emite o rastreamento de informações quando você ativa o rastreamento de rede em seu aplicativo. Para obter mais informações, consulte Rastreamento de rede no .NET Framework.

Aplica-se a