HttpListener.Start Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Permite que esta instância receba pedidos recebidos.
public:
void Start();
public void Start();
member this.Start : unit -> unit
Public Sub Start ()
Exceções
Uma chamada de função Win32 falhou. Verifique a propriedade da ErrorCode exceção para determinar a causa da dela.
Este objeto está fechado.
Exemplos
O exemplo de código seguinte demonstra a utilização do Start método para começar a processar pedidos recebidos.
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
Observações
Este método deve ser chamado antes de chamar o GetContext método ou BeginGetContext .
Depois de iniciar um HttpListener objeto, pode usar o Stop método para o parar.
Note
Se esta instância de ouvinte usar https, deve instalar e selecionar um Certificado de Servidor. Caso contrário, uma HttpWebRequest consulta desta HttpListener falha com um fecho inesperado da ligação. Pode configurar Certificados de Servidor e outras opções de ouvinte usando HttpCfg.exe.
Notas para Chamadores
Esse membro gera informações de rastreamento quando você habilita o rastreamento de rede em seu aplicativo. Para mais informações, consulte Rastreamento de Rede no .NET Framework.