Share via


HttpCookieCollection クラス

複数の HTTP cookie をタイプ セーフな方法で操作できるようにします。

名前空間: System.Web
アセンブリ: System.Web (system.web.dll 内)

構文

'宣言
Public NotInheritable Class HttpCookieCollection
    Inherits NameObjectCollectionBase
'使用
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
適用できません。

使用例

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

継承階層

System.Object
   System.Collections.Specialized.NameObjectCollectionBase
    System.Web.HttpCookieCollection

スレッド セーフ

この型の public static (Visual Basicでは共有) メンバはすべて,スレッド セーフです。インスタンス メンバの場合は,スレッド セーフであるとは限りません。

プラットフォーム

Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition

Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

バージョン情報

.NET Framework

サポート対象 : 3.0,2.0,1.1,1.0

参照

関連項目

HttpCookieCollection メンバ
System.Web 名前空間
HttpCookie

その他の技術情報

ASP.NET Cookie