HttpCookieCollection クラス
アセンブリ: System.Web (system.web.dll 内)
構文
HttpRequest オブジェクトの Cookies プロパティを使用して Cookie を読み取り、HttpResponse オブジェクトの Cookies プロパティを使用して Cookie を書き込む方法を次のコード例に示します。いずれのプロパティも HttpCookieCollection オブジェクトを返します。userName および lastVisit という名前の 2 つの Cookie のいずれかが HTTP 要求内に存在しないときは、HTTP 応答内に作成されます。この 2 つの Cookie が存在するときは、各 Cookie のプロパティが表示されます。
<%@ 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 > <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 > <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>
System.Collections.Specialized.NameObjectCollectionBase
System.Web.HttpCookieCollection
プラットフォーム
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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- HttpCookieCollection クラスのページへのリンク