HttpListener.Start Método

Definición

Permite que esta instancia reciba solicitudes de entrada.

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

Excepciones

Se ha producido un error en una llamada de función de Win32. Examine la propiedad ErrorCode de la excepción para determinar su causa.

Este objeto está cerrado.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar el Start método para comenzar a procesar las solicitudes entrantes.


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

Comentarios

Se debe llamar a este método antes de llamar al GetContext método o BeginGetContext .

Después de iniciar un HttpListener objeto, puede usar el Stop método para detenerlo.

Nota

Si esta instancia de agente de escucha usa https, debe instalar y seleccionar un certificado de servidor. De lo contrario, se producirá un error en una HttpWebRequest consulta con HttpListener un cierre inesperado de la conexión. Puede configurar certificados de servidor y otras opciones de agente de escucha mediante HttpCfg.exe.

Notas a los autores de las llamadas

Este miembro genera información de seguimiento cuando se habilita el seguimiento de red en la aplicación. Para obtener más información, vea Seguimiento de red en .NET Framework.

Se aplica a