HttpListenerContext Klasse
Definition
Ermöglicht den Zugriff auf die von der HttpListener-Klasse verwendeten Anforderungs- und Antwortobjekte.Provides access to the request and response objects used by the HttpListener class. Diese Klasse kann nicht vererbt werden.This class cannot be inherited.
public ref class HttpListenerContext sealed
public sealed class HttpListenerContext
type HttpListenerContext = class
Public NotInheritable Class HttpListenerContext
- Vererbung
-
HttpListenerContext
Beispiele
Im folgenden Codebeispiel werden die Benutzerinformationen für eine Client Anforderung angezeigt.The following code example displays the user information for a client request.
public static string ClientInformation(HttpListenerContext context)
{
System.Security.Principal.IPrincipal user = context.User;
System.Security.Principal.IIdentity id = user.Identity;
if (id == null)
{
return "Client authentication is not enabled for this Web server.";
}
string display;
if (id.IsAuthenticated)
{
display = String.Format("{0} was authenticated using {1}", id.Name,
id.AuthenticationType);
}
else
{
display = String.Format("{0} was not authenticated", id.Name);
}
return display;
}
Public Shared Function ClientInformation(ByVal context As HttpListenerContext) As String
Dim user As System.Security.Principal.IPrincipal = context.User
Dim id As System.Security.Principal.IIdentity = user.Identity
If id Is Nothing Then
Return "Client authentication is not enabled for this Web server."
End If
Dim display As String
If id.IsAuthenticated Then
display = String.Format("{0} was authenticated using {1}", id.Name, id.AuthenticationType)
Else
display = String.Format("{0} was not authenticated", id.Name)
End If
Return display
End Function
Hinweise
Diese Klasse stellt die Informationen im Zusammenhang mit der HTTP-Anforderung eines Clients bereit, die von einem HttpListener -Objekt verarbeitet wird.This class provides the information related to a client's Hypertext Transfer Protocol (HTTP) request being processed by an HttpListener object. Diese Klasse verfügt auch über Methoden, die HttpListener einem Objekt gestatten, eine WebSocket-Verbindung zu akzeptieren.This class also has methods that allow an HttpListener object to accept a WebSocket connection.
Die GetContext -Methode gibt Instanzen HttpListenerContext der-Klasse zurück.The GetContext method returns instances of the HttpListenerContext class. Um das-Objekt zu erhalten, das die Client Anforderung darstellt Request , verwenden Sie die-Eigenschaft.To get the object that represents the client request, use the Request property. Verwenden Sie die Response -Eigenschaft, um das-Objekt zu erhalten, das die Antwort darstellt HttpListener, die vom-Client an den Client gesendet wird.To get the object that represents the response that will be sent to the client by the HttpListener, use the Response property. Um Benutzerinformationen über den Client zu erhalten, der die Anforderung sendet, z. b. den Anmelde Namen und ob er authentifiziert wurde, können Sie die IPrincipal Eigenschaften im Objekt Abfragen User , das von der-Eigenschaft zurückgegeben wird.To get user information about the client sending the request, such as its login name and whether it has been authenticated, you can query the properties in the IPrincipal object returned by the User property.
Durch das HttpListenerContext Schließen eines Objekts wird die Antwort an den Client gesendet, und alle von verwendeten Ressourcen werden freigegeben. HttpListenerContextClosing an HttpListenerContext object sends the response to the client and frees any resources used by the HttpListenerContext. Das Abbrechen eines HttpListenerContext Objekts verwirft die Antwort an den Client, wenn er nicht bereits gesendet wurde, und gibt alle HttpListenerContextvon verwendeten Ressourcen frei.Aborting an HttpListenerContext object discards the response to the client if it has not already been sent, and frees any resources used by the HttpListenerContext. Nachdem ein HttpListenerContext Objekt geschlossen oder abgebrochen wurde, ist es nicht möglich, auf seine Methoden oder Eigenschaften zu verweisen.After closing or aborting an HttpListenerContext object, you cannot reference its methods or properties. Wenn Sie dies tun, erhalten Sie eine ObjectDisposedException Ausnahme.If you do so, you will receive an ObjectDisposedException exception.
Eigenschaften
Request |
Ruft die HttpListenerRequest ab, die die Anforderung eines Clients für eine Ressource darstellt.Gets the HttpListenerRequest that represents a client's request for a resource. |
Response |
Ruft das HttpListenerResponse-Objekt ab, das als Antwort auf die Anforderung des Clients an den Client gesendet wird.Gets the HttpListenerResponse object that will be sent to the client in response to the client's request. |
User |
Ruft ein Objekt ab, das zum Abrufen von Identitäts- und Sicherheitsinformationen sowie von Sicherheitsrollen für den Client verwendet wird, dessen Anforderung durch dieses HttpListenerContext-Objekt dargestellt wird.Gets an object used to obtain identity, authentication information, and security roles for the client whose request is represented by this HttpListenerContext object. |
Methoden
AcceptWebSocketAsync(String) |
Nehmen Sie eine WebSocket-Verbindung als asynchroner Vorgang an.Accept a WebSocket connection as an asynchronous operation. |
AcceptWebSocketAsync(String, Int32, TimeSpan) |
Nehmen Sie eine WebSocket-Verbindung an, die das unterstützte WebSocket-Unterprotokoll, empfangene Puffergröße und das WebSocket Keep-Alive-Intervall als asynchroner Vorgang angibt.Accept a WebSocket connection specifying the supported WebSocket sub-protocol, receive buffer size, and WebSocket keep-alive interval as an asynchronous operation. |
AcceptWebSocketAsync(String, Int32, TimeSpan, ArraySegment<Byte>) |
Nehmen Sie eine WebSocket-Verbindung an, die das unterstützte WebSocket-Unterprotokoll, empfangene Puffergröße, das WebSocket Keep-Alive-Intervall und den internen Puffer als asynchroner Vorgang angibt.Accept a WebSocket connection specifying the supported WebSocket sub-protocol, receive buffer size, WebSocket keep-alive interval, and the internal buffer as an asynchronous operation. |
AcceptWebSocketAsync(String, TimeSpan) |
Nehmen Sie eine WebSocket-Verbindung an, die das unterstützte WebSocket-Unterprotokoll und das WebSocket Keep-Alive-Intervall als asynchroner Vorgang angibt.Accept a WebSocket connection specifying the supported WebSocket sub-protocol and WebSocket keep-alive interval as an asynchronous operation. |
Equals(Object) |
Ermittelt, ob das angegebene Objekt und das aktuelle Objekt gleich sind.Determines whether the specified object is equal to the current object. (Geerbt von Object) |
GetHashCode() |
Dient als die Standard-HashfunktionServes as the default hash function. (Geerbt von Object) |
GetType() |
Ruft den Type der aktuellen Instanz ab.Gets the Type of the current instance. (Geerbt von Object) |
MemberwiseClone() |
Erstellt eine flache Kopie des aktuellen Object.Creates a shallow copy of the current Object. (Geerbt von Object) |
ToString() |
Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt.Returns a string that represents the current object. (Geerbt von Object) |