HttpListener Classe

Definizione

Fornisce un listener del protocollo HTTP semplice e 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 supplementari sull'API per HttpListener.

Costruttori

Nome Descrizione
HttpListener()

Inizializza una nuova istanza della classe HttpListener.

Proprietà

Nome Descrizione
AuthenticationSchemes

Ottiene o imposta lo schema utilizzato per autenticare i client.

AuthenticationSchemeSelectorDelegate

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

DefaultServiceNames

Ottiene un elenco predefinito dei nomi dei provider di servizi (SPN) come determinato dai prefissi registrati.

ExtendedProtectionPolicy

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

ExtendedProtectionSelectorDelegate

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

IgnoreWriteExceptions

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

IsListening

Ottiene un valore che indica se HttpListener è stato avviato.

IsSupported

Ottiene un valore che indica se HttpListener è possibile utilizzare con il sistema operativo corrente.

Prefixes

Ottiene i prefissi URI (Uniform Resource Identifier) gestiti da questo HttpListener oggetto.

Realm

Ottiene o imposta l'area di autenticazione o la partizione di risorsa associata a questo HttpListener oggetto.

TimeoutManager

Gestore del timeout per questa HttpListener istanza.

UnsafeConnectionNtlmAuthentication

Ottiene o imposta un Boolean valore che controlla se, quando viene utilizzato NTLM, sono necessarie richieste aggiuntive che usano la stessa connessione TCP (Transmission Control Protocol).

Metodi

Nome Descrizione
Abort()

Arresta immediatamente l'oggetto HttpListener , ignorando tutte le richieste attualmente in coda.

BeginGetContext(AsyncCallback, Object)

Inizia a recuperare in modo asincrono una richiesta in ingresso.

Close()

Arresta l'oggetto HttpListener.

EndGetContext(IAsyncResult)

Completa un'operazione asincrona per recuperare una richiesta 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 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 il Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale del Objectcorrente.

(Ereditato da Object)
Start()

Consente a questa istanza di ricevere richieste in ingresso.

Stop()

Causa l'interruzione della ricezione di nuove richieste in ingresso da parte di questa istanza 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

Nome Descrizione
IDisposable.Dispose()

Rilascia le risorse contenute in questo HttpListener oggetto.

Si applica a

Vedi anche