加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何编程测试Cookie?

发布时间:2020-12-15 06:54:34 所属栏目:百科 来源:网络整理
导读:如何检查/检测用户是否接受cookies?使用ASP.NET(C#) 解决方法 从 MSDN One way to determine whether cookies are accepted is by trying to write a cookie and then trying to read it back again. If you can’t read the cookie you wrote,you assume t
如何检查/检测用户是否接受cookies?使用ASP.NET(C#)

解决方法

从 MSDN

One way to determine whether cookies are accepted is by trying to write a cookie and then trying to read it back again. If you can’t read the cookie you wrote,you assume that cookies are turned off in the browser.

写:

Sub Page_Load()
   If Not Page.IsPostBack Then
      If Request.QueryString("AcceptsCookies") Is Nothing Then
           Response.Cookies("TestCookie").Value = "ok"
           Response.Cookies("TestCookie").Expires = _
              DateTime.Now.AddMinutes(1)
           Response.Redirect("TestForCookies.aspx?redirect=" & _
              Server.UrlEncode(Request.Url.ToString))
      Else
           labelAcceptsCookies.Text = "Accept cookies = " & _
              Request.QueryString("AcceptsCookies")
      End If
   End If
End Sub

然后阅读

Sub Page_Load()
    Dim redirect As String = Request.QueryString("redirect")
    Dim acceptsCookies As String
    ' Was the cookie accepted?
    If Request.Cookies("TestCookie") Is Nothing Then
        ' No cookie,so it must not have been accepted
        acceptsCookies = 0
    Else
        acceptsCookies = 1
        ' Delete test cookie
        Response.Cookies("TestCookie").Expires = _
           DateTime.Now.AddDays(-1)
    End If
    Response.Redirect(redirect & "?AcceptsCookies=" & acceptsCookies,_
       True)
End Sub

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读