HttpCookie 类

HttpCookie :提供创建和操作各 HTTP Cookie 的类型安全方法。

下面的代码示例演示如何对 HttpRequest 对象中名为 DateCookieExample Cookie 进行检查。如果找不到该 Cookie,则将创建它并将其添加到 HttpResponse 对象。Cookie 设置为 10 分钟后过期。

<%@ Page Language="C#" %>

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)

    {

        StringBuilder sb = new StringBuilder();

        // Get cookie from the current request.

        HttpCookie cookie = Request.Cookies.Get("DateCookieExample");

 

        // Check if cookie exists in the current request.

        if (cookie == null)

        {

            sb.Append("Cookie was not received from the client. ");

            sb.Append("Creating cookie to add to the response. <br/>");

            // Create cookie.

            cookie = new HttpCookie("DateCookieExample");

            // Set value of cookie to current date time.

            cookie.Value = DateTime.Now.ToString();

            // Set cookie to expire in 10 minutes.

            cookie.Expires = DateTime.Now.AddMinutes(10d);

            // Insert the cookie in the current HttpResponse.

            Response.Cookies.Add(cookie);

        }

        else

        {

            sb.Append("Cookie retrieved from client. <br/>");

            sb.Append("Cookie Name: " + cookie.Name + "<br/>");

            sb.Append("Cookie Value: " + cookie.Value + "<br/>");

            sb.Append("Cookie Expiration Date: " +

                cookie.Expires.ToString() + "<br/>");

        }

        Label1.Text = sb.ToString();

    }

</script>

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>HttpCookie Example</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

      <asp:Label id="Label1" runat="server"></asp:Label>

    </div>

    </form>

</body>

</html>

 

下面的代码示例演示如何使用 HttpRequest 对象的 Cookies 属性读取 Cookie 以及如何使用 HttpResponse 对象的 Cookies 属性写入 Cookie。这两个属性均返回 HttpCookieCollection 对象。如果两个 Cookie 中的任何一个命名为 userName,且 lastVisit 不在 HTTP 请求中,则在 HTTP 响应中创建这两个 Cookie。如果两个 Cookie 存在,则显示这两个 Cookie 的属性。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值