HttpCookieCollection クラスとは? わかりやすく解説

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  >
<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.Object
   System.Collections.Specialized.NameObjectCollectionBase
    System.Web.HttpCookieCollection
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からHttpCookieCollection クラスを検索した結果を表示しています。
Weblioに収録されているすべての辞書からHttpCookieCollection クラスを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からHttpCookieCollection クラス を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「HttpCookieCollection クラス」の関連用語

HttpCookieCollection クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



HttpCookieCollection クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS