• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

.net beta2 操作cookie的例子(转)

发布: 2007-7-01 11:09 | 作者: admin | 来源: | 查看: 47次 | 进入软件测试论坛讨论

领测软件测试网 <%@ Page Language="VB" %>

<script language="VB" runat="server">
    Const COOKIE_NAME  As String = "test-cookie-name"
    Const COOKIE_VALUE As String = "test-cookie-value"

    @# Declare our cookie object
    Dim objCookieObject As HttpCookie
    
    Sub btnSetCookie_OnClick(Sender As Object, E As EventArgs)
        @# Create a cookie object - I@#m passing name and value,
        @# but you can also pass in a name and set the value later.
        @# ie. objCookieObject = New HttpCookie(COOKIE_NAME)
        objCookieObject = New HttpCookie(COOKIE_NAME, COOKIE_VALUE)

        @# We already set these above!
        @#objCookieObject.Name   = COOKIE_NAME
        @#objCookieObject.Value  = COOKIE_VALUE

        @# Additional cookie properties:
        objCookieObject.Expires = New DateTime(2001, 11, 12)

        @# Normally you can leave these alone.
        @# The defaults will work fine for most uses.
        @#objCookieObject.Domain  = "www.domain.com"
        @#objCookieObject.Path    = "/path/"
        @#objCookieObject.Secure  = True
        
        Response.AppendCookie(objCookieObject)
    End Sub

    Sub btnRemoveCookie_OnClick(Sender As Object, E As EventArgs)
        objCookieObject = New HttpCookie(COOKIE_NAME)
        
        @# Expire it on the day I was born just so we@#re sure it@#s a date in the past.
        objCookieObject.Expires = New DateTime(1974, 11, 12)
        
        Response.AppendCookie(objCookieObject)
    End Sub

    Sub btnGetCookie_OnClick(Sender As Object, E As EventArgs)
        objCookieObject = Request.Cookies(COOKIE_NAME)

        @# In Beta 1 this worked fine... not with Beta 2.
        @#If Not(objCookieObject = Nothing) Then
        
        If Not(objCookieObject Is Nothing) Then
            lblCookieDetails.Text        = objCookieObject.Name

            lblCookieDetailsName.Text    = objCookieObject.Name
            lblCookieDetailsValue.Text   = objCookieObject.Value
            
            @# For some reason I@#m having trouble with this now...
            @# The setting seems to work, but I can@#t read it back.
            @# Anyone have any ideas? Email me -> john@asp101.com
            lblCookieDetailsExpires.Text = objCookieObject.Expires.ToString
            
            lblCookieDetailsDomain.Text  = objCookieObject.Domain
            lblCookieDetailsPath.Text    = objCookieObject.Path
            lblCookieDetailsSecure.Text  = objCookieObject.Secure.ToString
            lblCookieDetailsHasKeys.Text = objCookieObject.HasKeys.ToString
        Else
            lblCookieDetails.Text        = "Cookie Not Set!"

            lblCookieDetailsName.Text    = ""
            lblCookieDetailsValue.Text   = ""
            lblCookieDetailsExpires.Text = ""
            lblCookieDetailsDomain.Text  = ""
            lblCookieDetailsPath.Text    = ""
            lblCookieDetailsSecure.Text  = ""
            lblCookieDetailsHasKeys.Text = ""
        End If

        @# I@#m ignoring collections.  They@#re outside the realm of this basic sample.
        @# FYI: Additional properties related to cookie collections: Values, Item
    End Sub
</script>

<html>
<body>

<h4>The cookie name we@#re using for this sample is: <em><%= COOKIE_NAME %></em></h4>

<form action="cookie.aspx" method="post" runat="server">
    <asp:Button type="submit" id="btnSetCookie" text="Set Cookie" OnClick="btnSetCookie_OnClick" runat="server" />
    <asp:Button type="submit" id="btnRemoveCookie" text="Remove Cookie" OnClick="btnRemoveCookie_OnClick" runat="server" />

    <p>
    To see the cookie@#s current status you@#ll need to click below.  This is because the response which adds or deletes the cookie happens after the request is already done.  As such, those changes aren@#t available from the request collection until the next request.
    </p>

    <asp:Button type="submit" id="btnGetCookie" text="Get Cookie Details" OnClick="btnGetCookie_OnClick" runat="server" />
</form>

<p>
<strong>Details of:</strong> <asp:label id="lblCookieDetails" runat="server" />
</p>

<table border="1">
    <thead>
        <tr>
            <th>Property</th>
            <th>Value</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Name</td>
            <td><asp:label id="lblCookieDetailsName" runat="server" /></td>
        </tr>
        <tr>
            <td>Value</td>
            <td><asp:label id="lblCookieDetailsValue" runat="server" /></td>
        </tr>
        <tr>
            <td>Expires</td>
            <td><asp:label id="lblCookieDetailsExpires" runat="server" /></td>
        </tr>
        <tr>
            <td>Domain</td>
            <td><asp:label id="lblCookieDetailsDomain" runat="server" /></td>
        </tr>
        <tr>
            <td>Path</td>
            <td><asp:label id="lblCookieDetailsPath" runat="server" /></td>
        </tr>
        <tr>
            <td>Secure</td>
            <td><asp:label id="lblCookieDetailsSecure" runat="server" /></td>
        </tr>
        <tr>
            <td>Has Keys</td>
            <td><asp:label id="lblCookieDetailsHasKeys" runat="server" /></td>
        </tr>
    </tbody>
</table>

</body>
</html>
----------------------------------------------------
come from Asp101.com

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网