HttpListenerRequest Classe
Définition
Décrit une demande HTTP entrante d'un objet HttpListener.Describes an incoming HTTP request to an HttpListener object. Cette classe ne peut pas être héritée.This class cannot be inherited.
public ref class HttpListenerRequest sealed
public sealed class HttpListenerRequest
type HttpListenerRequest = class
Public NotInheritable Class HttpListenerRequest
- Héritage
-
HttpListenerRequest
Exemples
L’exemple de code suivant montre comment recevoir un HttpListenerRequest et y répondre.The following code example demonstrates how to receive and respond to a HttpListenerRequest.
// 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
Remarques
Lorsqu’un client envoie une demande à un Uniform Resource Identifier (URI) géré par un objet HttpListener, le HttpListener fournit un objet HttpListenerContext qui contient des informations sur l’expéditeur, la demande et la réponse envoyée au client.When a client makes a request to a Uniform Resource Identifier (URI) handled by an HttpListener object, the HttpListener provides a HttpListenerContext object that contains information about the sender, the request, and the response that is sent to the client. La propriété HttpListenerContext.Request retourne l’objet HttpListenerRequest qui décrit la requête.The HttpListenerContext.Request property returns the HttpListenerRequest object that describes the request.
L’objet HttpListenerRequest contient des informations sur la demande, telles que la chaîne de demande HttpMethod, la chaîne UserAgent et les données du corps de la demande (voir la propriété InputStream).The HttpListenerRequest object contains information about the request, such as the request HttpMethod string, UserAgent string, and request body data (see the InputStream property).
Pour répondre à la demande, vous devez obtenir la réponse associée à l’aide de la propriété Response.To reply to the request, you must get the associated response using the Response property.
Propriétés
AcceptTypes |
Obtient les types MIME acceptés par le client.Gets the MIME types accepted by the client. |
ClientCertificateError |
Obtient un code d'erreur qui identifie un problème avec le X509Certificate fourni par le client.Gets an error code that identifies a problem with the X509Certificate provided by the client. |
ContentEncoding |
Obtient l’encodage du contenu qui peut être utilisé pour les données envoyées avec la demande.Gets the content encoding that can be used with data sent with the request. |
ContentLength64 |
Obtient la longueur des données relatives au corps incluse dans la demande.Gets the length of the body data included in the request. |
ContentType |
Obtient le type MIME des données relatives au corps compris dans la demande.Gets the MIME type of the body data included in the request. |
Cookies |
Obtient les cookies envoyés avec la demande.Gets the cookies sent with the request. |
HasEntityBody |
Obtient une valeur Boolean qui indique si la demande possède des données relatives au corps associées.Gets a Boolean value that indicates whether the request has associated body data. |
Headers |
Obtient la collection de paires nom/valeur d'en-tête envoyées dans la demande.Gets the collection of header name/value pairs sent in the request. |
HttpMethod |
Obtient la méthode HTTP spécifiée par le client.Gets the HTTP method specified by the client. |
InputStream |
Obtient un flux contenant les données relatives au corps envoyées par le client.Gets a stream that contains the body data sent by the client. |
IsAuthenticated |
Obtient une valeur Boolean qui indique si le client qui envoie cette demande est authentifié.Gets a Boolean value that indicates whether the client sending this request is authenticated. |
IsLocal |
Obtient une valeur Boolean indiquant si la demande est envoyée par l'ordinateur local.Gets a Boolean value that indicates whether the request is sent from the local computer. |
IsSecureConnection |
Obtient une valeur Boolean indiquant si la connexion TCP utilisée pour envoyer la demande utilise le protocole SSL (Secure Sockets Layer).Gets a Boolean value that indicates whether the TCP connection used to send the request is using the Secure Sockets Layer (SSL) protocol. |
IsWebSocketRequest |
Obtient une valeur Boolean qui indique si la connexion TCP était une requête WebSocket.Gets a Boolean value that indicates whether the TCP connection was a WebSocket request. |
KeepAlive |
Obtient une valeur Boolean qui indique si le client exige une connexion persistante.Gets a Boolean value that indicates whether the client requests a persistent connection. |
LocalEndPoint |
Obtient l'adresse IP et le numéro de port du serveur vers lequel la demande est dirigée.Gets the server IP address and port number to which the request is directed. |
ProtocolVersion |
Obtient la version HTTP utilisée par le client demandeur.Gets the HTTP version used by the requesting client. |
QueryString |
Obtient la chaîne de requête incluse dans la demande.Gets the query string included in the request. |
RawUrl |
Obtient les informations d'URL (sans l'hôte et le port) demandées par le client.Gets the URL information (without the host and port) requested by the client. |
RemoteEndPoint |
Obtient l'adresse IP et le numéro de port du client à l'origine de la demande.Gets the client IP address and port number from which the request originated. |
RequestTraceIdentifier |
Obtient l'identificateur de la demande HTTP entrante.Gets the request identifier of the incoming HTTP request. |
ServiceName |
Obtient le nom du fournisseur de services (SPN) que le client a envoyé sur la requête.Gets the Service Provider Name (SPN) that the client sent on the request. |
TransportContext |
Obtient le TransportContext de la requête client.Gets the TransportContext for the client request. |
Url |
Obtient l'objet Uri demandé par le client.Gets the Uri object requested by the client. |
UrlReferrer |
Obtient l'URI (Uniform Resource Identifier) de la ressource qui a signalé le client au serveur.Gets the Uniform Resource Identifier (URI) of the resource that referred the client to the server. |
UserAgent |
Obtient l'agent utilisateur (user agent) présenté par le client.Gets the user agent presented by the client. |
UserHostAddress |
Obtient l'adresse IP et le numéro de port du serveur vers lequel la demande est dirigée.Gets the server IP address and port number to which the request is directed. |
UserHostName |
Obtient le nom DNS et, s'il est fourni, le numéro de port spécifié par le client.Gets the DNS name and, if provided, the port number specified by the client. |
UserLanguages |
Obtient les langages naturels préférés pour la réponse.Gets the natural languages that are preferred for the response. |
Méthodes
BeginGetClientCertificate(AsyncCallback, Object) |
Commence une demande asynchrone du certificat X.509 v.3 du client.Begins an asynchronous request for the client's X.509 v.3 certificate. |
EndGetClientCertificate(IAsyncResult) |
Met fin à une demande asynchrone du certificat X.509 v.3 du client.Ends an asynchronous request for the client's X.509 v.3 certificate. |
Equals(Object) |
Détermine si l'objet spécifié est égal à l'objet actuel.Determines whether the specified object is equal to the current object. (Hérité de Object) |
GetClientCertificate() |
Récupère le certificat X.509 v.3 du client.Retrieves the client's X.509 v.3 certificate. |
GetClientCertificateAsync() |
Récupère le certificat X.509 v.3 du client comme une opération asynchrone.Retrieves the client's X.509 v.3 certificate as an asynchronous operation. |
GetHashCode() |
Sert de fonction de hachage par défaut.Serves as the default hash function. (Hérité de Object) |
GetType() |
Obtient le Type de l'instance actuelle.Gets the Type of the current instance. (Hérité de Object) |
MemberwiseClone() |
Crée une copie superficielle du Object actuel.Creates a shallow copy of the current Object. (Hérité de Object) |
ToString() |
Retourne une chaîne qui représente l'objet en cours.Returns a string that represents the current object. (Hérité de Object) |