HttpListener Classe

Definizione

Fornisce un listener semplice del protocollo HTTP controllato a livello di codice. La classe non può essere ereditata.

public ref class HttpListener sealed : IDisposable
public sealed class HttpListener : IDisposable
type HttpListener = class
    interface IDisposable
Public NotInheritable Class HttpListener
Implements IDisposable
Ereditarietà
HttpListener
Implementazioni

Esempio

// This example requires the System and System.Net namespaces.
public static void SimpleListenerExample(string[] prefixes)
{
    if (!HttpListener.IsSupported)
    {
        Console.WriteLine ("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
        return;
    }
    // URI prefixes are required,
    // for example "http://contoso.com:8080/index/".
    if (prefixes == null || prefixes.Length == 0)
      throw new ArgumentException("prefixes");

    // Create a listener.
    HttpListener listener = new HttpListener();
    // Add the prefixes.
    foreach (string s in prefixes)
    {
        listener.Prefixes.Add(s);
    }
    listener.Start();
    Console.WriteLine("Listening...");
    // Note: The GetContext method blocks while waiting for a request.
    HttpListenerContext context = listener.GetContext();
    HttpListenerRequest request = context.Request;
    // Obtain a response object.
    HttpListenerResponse response = context.Response;
    // Construct a response.
    string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
    // Get a response stream and write the response to it.
    response.ContentLength64 = buffer.Length;
    System.IO.Stream output = response.OutputStream;
    output.Write(buffer,0,buffer.Length);
    // You must close the output stream.
    output.Close();
    listener.Stop();
}
Public Shared Sub SimpleListenerExample(prefixes As String())
    If Not HttpListener.IsSupported Then
        Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.")
        Return
    End If
    ' URI prefixes are required,
    ' for example "http://contoso.com:8080/index/".
    If prefixes Is Nothing Or prefixes.Length = 0 Then
        Throw New ArgumentException("prefixes")
    End If

    ' Create a listener
    Dim listener = New HttpListener()

    For Each s As String In prefixes
        listener.Prefixes.Add(s)
    Next
    listener.Start()
    Console.WriteLine("Listening...")
    ' Note: The GetContext method blocks while waiting for a request.
    Dim context As HttpListenerContext = listener.GetContext()
    Console.WriteLine("Listening...")
    ' Obtain a response object
    Dim request As HttpListenerRequest = context.Request
    ' Construct a response.
    Dim response As HttpListenerResponse = context.Response
    Dim responseString As String = "<HTML><BODY> Hello world!</BODY></HTML>"
    Dim buffer As Byte() = System.Text.Encoding.UTF8.GetBytes(responseString)
    ' Get a response stream and write the response to it.
    response.ContentLength64 = buffer.Length
    Dim output As System.IO.Stream = response.OutputStream
    output.Write(buffer, 0, buffer.Length)
    'You must close the output stream.
    output.Close()
    listener.Stop()
End Sub

Commenti

Per altre informazioni su questa API, vedere Osservazioni api supplementari per HttpListener.

Costruttori

HttpListener()

Inizializza una nuova istanza della classe HttpListener.

Proprietà

AuthenticationSchemes

Ottiene o imposta lo schema usato per autenticare i client.

AuthenticationSchemeSelectorDelegate

Ottiene o imposta il delegato chiamato per determinare il protocollo usato per autenticare i client.

DefaultServiceNames

Ottiene un elenco predefinito di nomi di provider di servizi come determinato dai prefissi registrati.

ExtendedProtectionPolicy

Ottiene o imposta l'oggetto ExtendedProtectionPolicy da usare per la protezione estesa di una sessione.

ExtendedProtectionSelectorDelegate

Ottiene o imposta il delegato chiamato per determinare l'oggetto ExtendedProtectionPolicy da usare per ogni richiesta.

IgnoreWriteExceptions

Ottiene o imposta un valore Boolean che specifica se l'applicazione riceve le eccezioni che si verificano quando un oggetto HttpListener invia la risposta al client.

IsListening

Ottiene un valore che indica se l'oggetto HttpListener è stato avviato.

IsSupported

Ottiene un valore che indica se l'oggetto HttpListener può essere usato con il sistema operativo corrente.

Prefixes

Ottiene i prefissi URI (Uniform Resource Identifier) gestiti dall'oggetto HttpListener.

Realm

Ottiene o imposta l'area di autenticazione, o partizione di risorsa, associata all'oggetto HttpListener.

TimeoutManager

Gestore di timeout per questa istanza di HttpListener.

UnsafeConnectionNtlmAuthentication

Ottiene o imposta un valore Boolean che controlla se è necessario autenticare altre richieste che usano la stessa connessione TCP (Transmission Control Protocol), quando viene usato NTLM.

Metodi

Abort()

Arresta immediatamente l'oggetto HttpListener annullando tutte le richieste correntemente accodate.

BeginGetContext(AsyncCallback, Object)

Inizia il recupero di una richiesta in ingresso in modo asincrono.

Close()

Chiude HttpListener.

EndGetContext(IAsyncResult)

Completa un'operazione asincrona per recuperare una richiesta del client in ingresso.

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetContext()

Attende una richiesta in ingresso e restituisce un risultato quando ne viene ricevuta una.

GetContextAsync()

Attende una richiesta in ingresso come operazione asincrona.

GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
Start()

Consente a questa istanza di ricevere le richieste in ingresso.

Stop()

Fa in modo che questa istanza interrompa la ricezione delle nuove richieste in ingresso e termina l'elaborazione di tutte le richieste in corso.

ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Implementazioni dell'interfaccia esplicita

IDisposable.Dispose()

Rilascia le risorse contenute nell'oggetto HttpListener.

Si applica a

Vedi anche