HttpListenerRequest Classe

Definizione

Descrive una richiesta HTTP in arrivo a un oggetto HttpListener. La classe non può essere ereditata.

public ref class HttpListenerRequest sealed
public sealed class HttpListenerRequest
type HttpListenerRequest = class
Public NotInheritable Class HttpListenerRequest
Ereditarietà
HttpListenerRequest

Esempio

Nell'esempio di codice seguente viene illustrato come ricevere e rispondere a un HttpListenerRequestoggetto .

// 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

Quando un client effettua una richiesta a un URI (Uniform Resource Identifier) gestito da un HttpListener oggetto, fornisce un HttpListenerContext oggetto contenente informazioni sul mittente, HttpListener sulla richiesta e sulla risposta inviata al client. La HttpListenerContext.Request proprietà restituisce l'oggetto HttpListenerRequest che descrive la richiesta.

L'oggetto HttpListenerRequest contiene informazioni sulla richiesta, ad esempio la stringa di richiesta, UserAgent la stringa e i HttpMethod dati del corpo della richiesta (vedere la InputStream proprietà).

Per rispondere alla richiesta, è necessario ottenere la risposta associata usando la Response proprietà .

Proprietà

AcceptTypes

Ottiene i tipi MIME accettati dal client.

ClientCertificateError

Ottiene un codice di errore che identifica un problema con la classe X509Certificate fornita dal client.

ContentEncoding

Ottiene la codifica del contenuto che può essere usata con i dati inviati con la richiesta.

ContentLength64

Ottiene la lunghezza dei dati del corpo inclusi nella richiesta.

ContentType

Ottiene il tipo MIME dei dati del corpo inclusi nella richiesta.

Cookies

Ottiene i cookie inviati con la richiesta.

HasEntityBody

Ottiene un valore Boolean che indica se alla richiesta sono associati i dati del corpo.

Headers

Ottiene un insieme delle coppie nome/valore di intestazione inviate nella risposta.

HttpMethod

Ottiene il metodo HTTP specificato dal client.

InputStream

Ottiene un flusso contenente i dati del corpo inviati dal client.

IsAuthenticated

Ottiene un valore Boolean che indica se il client che invia la richiesta è autenticato.

IsLocal

Ottiene un valore Boolean che indica se la richiesta proviene dal computer locale.

IsSecureConnection

Ottiene un valore Boolean che indica se la connessione TCP utilizzata per inviare la richiesta utilizza il protocollo SSL (Secure Sockets Layer).

IsWebSocketRequest

Ottiene un valore Boolean che indica se la connessione TCP era una richiesta a WebSocket.

KeepAlive

Ottiene un valore Boolean che indica se dal client viene richiesta una connessione permanente.

LocalEndPoint

Ottiene l'indirizzo IP e il numero di porta del server a cui è diretta la richiesta.

ProtocolVersion

Ottiene la versione HTTP utilizzata dal client richiedente.

QueryString

Ottiene la stringa di query inclusa nella richiesta.

RawUrl

Ottiene le informazioni sull'URL (senza host e porta) richieste dal client.

RemoteEndPoint

Ottiene l'indirizzo IP e il numero di porta del client da cui ha avuto origine la richiesta.

RequestTraceIdentifier

Ottiene l'identificatore di richiesta della richiesta HTTP in ingresso.

ServiceName

Ottiene il nome del provider di servizi inviato dal client alla richiesta.

TransportContext

Ottiene l'oggetto TransportContext per la richiesta del client.

Url

Ottiene l'oggetto Uri richiesto dal client.

UrlReferrer

Ottiene l'URI della risorsa che ha associato il client al server.

UserAgent

Ottiene l'agente utente presentato dal client.

UserHostAddress

Ottiene l'indirizzo IP e il numero di porta del server a cui è diretta la richiesta.

UserHostName

Ottiene il nome DNS e, se fornito, il numero di porta specificato dal client.

UserLanguages

Ottiene i linguaggi naturali preferiti per la risposta.

Metodi

BeginGetClientCertificate(AsyncCallback, Object)

Consente di avviare una richiesta asincrona per il certificato X.509 v.3 del client.

EndGetClientCertificate(IAsyncResult)

Consente di terminare una richiesta asincrona per il certificato X.509 v.3 del client.

Equals(Object)

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

(Ereditato da Object)
GetClientCertificate()

Consente di recuperare il certificato X.509 v.3 del client.

GetClientCertificateAsync()

Recupera il certificato X.509 v.3 del client 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)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a

Vedi anche