ASP.NET-【狀態管理】-Cookie小結

來源:互聯網
上載者:User

標籤:

Cookie路徑Google瀏覽器依次點擊設定--進階選項--內容設定--cookies--選擇“顯示cookies和其他網站資料”按鈕就可以看到了

C:\Users\Administrator\Local Settings\Application Data\Google\Chrome\User Data\Default

IE瀏覽器
C:\Users\Administrator\AppData\Local\Microsoft\Windows\Temporary Internet Files 注意:只有設定有失效期的 cookie 在離開設定頁時會以檔案形式儲存 C:\Users\Administrator\CookiesC:\Users\Administrator\AppData\Local\Microsoft\Windows\Temporary Internet FilesC:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Cookies 查看Cookie

下面看看代碼就什麼都知道了我的總結都在下面
        protected void Page_Load(object sender, EventArgs e)        {            HttpCookie c1 = new HttpCookie("a", "123");            HttpCookie c2 = new HttpCookie("a", "456"); //注意覆蓋了同名的cookie             c2.Values.Add("e", "hello");//看看這個會是什麼效果呢? 哈哈讀取之後【js讀取】 是            //a=456&e=hello;c=789; 會把hello一起寫在Cookie “a”裡面去            if (Request.Cookies["a"] != null)            {                //Response.Write(Request.Cookies["a"].Value);//456&e=hello                //Response.Write(Request.Cookies["a"]["e"].ToString());//hello                System.Collections.Specialized.NameValueCollection d = Request.Cookies["a"].Values;                foreach (var item in d.AllKeys)                {                    Response.Write(item + ":" + d[item] + "<br />");                    //:456                    //e:hello                }            }            //設定一個到期時間            c2.Expires = DateTime.Now.AddMinutes(30);            HttpCookie c3 = new HttpCookie("c", "789");            //如果要寫入中文的話 請先編碼好            string ch = Server.UrlEncode("中文=");            HttpCookie c4 = new HttpCookie("d", ch);            HttpCookie c5 = new HttpCookie("t", "=");            Response.AppendCookie(c1);            Response.AppendCookie(c2);            Response.AppendCookie(c3);            Response.AppendCookie(c4);            Response.AppendCookie(c5);            //總結:每執行個體化一個Cookie對象就會在瀏覽器中建立一個Cookie檔案            //如果沒有設定cookie到期時間,那麼Cookie就是瀏覽器會話Cookie,瀏覽器關閉 Cookie就會消失            //如果執行個體化的Cookie有相同名字的,則後面的會覆蓋前面的            //看上面的代碼就知道怎麼回事兒了            //Values  Cookie集合Request.Cookies["a"]["e"].ToString() 通過這個方式追個訪問cookie的值            //Value   訪問寫入Cookie的所有值,            //哈哈現在 疑惑消除了        }
js讀取        <div>            <input type="button" value="ok" onclick="fun()" />        </div>        <script type="text/javascript">            function fun() {                document.write(decodeURIComponent(document.cookie));            }        </script> 

ASP.NET-【狀態管理】-Cookie小結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.