HttpCookieCollection-Klasse
Stellt ein typsicheres Verfahren für das Ändern von HTTP-Cookies bereit.
Namespace: System.Web
Assembly: System.Web (in system.web.dll)
Syntax
'Declaration
Public NotInheritable Class HttpCookieCollection
Inherits NameObjectCollectionBase
'Usage
Dim instance As HttpCookieCollection
public sealed class HttpCookieCollection : NameObjectCollectionBase
public ref class HttpCookieCollection sealed : public NameObjectCollectionBase
public final class HttpCookieCollection extends NameObjectCollectionBase
public final class HttpCookieCollection extends NameObjectCollectionBase
Beispiel
Im folgenden Codebeispiel wird veranschaulicht, wie Cookies mithilfe der Cookies-Eigenschaft des HttpRequest-Objektes zu lesen sind und wie man Cookies mithilfe der Cookies-Eigenschaft des HttpResponse-Objektes schreibt. Beide Eigenschaften geben HttpCookieCollection-Objekte zurück. Wenn einer von zwei Cookies namens userName
und lastVisit
nicht in der HTTP-Anforderung ist, werden sie in der HTTP-Antwort erstellt. Wenn die zwei Cookies vorhanden sind, werden die Eigenschaften der Cookies angezeigt.
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim sb As New StringBuilder()
' Check to see if cookies exist in HTTP request.
If (Request.Cookies("userName") Is Nothing AndAlso _
Request.Cookies("lastVisit") Is Nothing) Then
Response.Cookies("userName").Value = "user name"
Response.Cookies("userName").Expires = DateTime.Now.AddMinutes(20D)
Dim aCookie As HttpCookie
aCookie = New HttpCookie("lastVisit")
aCookie.Value = DateTime.Now.ToString()
aCookie.Expires = DateTime.Now.AddMinutes(20D)
Response.Cookies.Add(aCookie)
sb.Append("Two cookies added to response. " & _
"Refresh the page to read the cookies.")
Else
Dim cookies As HttpCookieCollection
cookies = Request.Cookies
For i As Integer = 0 To cookies.Count - 1
sb.Append("Name: " & cookies(i).Name & "<br/>")
sb.Append("Value: " & cookies(i).Value & "<br/>")
sb.Append("Expires: " & cookies(i).Expires.ToString() & _
"<br/><br/>")
Next
End If
Label1.Text = sb.ToString()
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>HttpCookieCollection Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
// Check to see if cookies exist in HTTP request.
if (Request.Cookies["userName"] == null &&
Request.Cookies["lastVist"] == null)
{
Response.Cookies["userName"].Value = "user name";
Response.Cookies["userName"].Expires = DateTime.Now.AddMinutes(20d);
HttpCookie aCookie = new HttpCookie("lastVisit");
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddMinutes(20d);
Response.Cookies.Add(aCookie);
sb.Append("Two cookies added to response. " +
"Refresh the page to read the cookies.");
}
else
{
HttpCookieCollection cookies = Request.Cookies;
for (int i = 0; i < cookies.Count; i++)
{
sb.Append("Name: " + cookies[i].Name + "<br/>");
sb.Append("Value: " + cookies[i].Value + "<br/>");
sb.Append("Expires: " + cookies[i].Expires.ToString() +
"<br/><br/>");
}
}
Label1.Text = sb.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>HttpCookieCollection Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
Vererbungshierarchie
System.Object
System.Collections.Specialized.NameObjectCollectionBase
System.Web.HttpCookieCollection
Threadsicherheit
Alle öffentlichen statischen (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.
Plattformen
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
Siehe auch
Referenz
HttpCookieCollection-Member
System.Web-Namespace
HttpCookie-Klasse