天天看点

vb.net cookie操作

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <%@ 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 obj cookieObject As Http cookie

    Sub btnSet cookie_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. obj cookieObject = New Http cookie( cookie_NAME)

        obj cookieObject = New Http cookie( cookie_NAME, cookie_VALUE)

        ' We already set these above!

        'obj cookieObject.Name   = cookie_NAME

        'obj cookieObject.Value  = cookie_VALUE

        ' Additional cookie properties:

        obj cookieObject.Expires = New DateTime(2010, 11, 12)

        ' Normally you can leave these alone.

        ' The defaults will work fine for most uses.

        'obj cookieObject.Domain  = "www.domain.com"

        'obj cookieObject.Path    = "/path/"

        'obj cookieObject.Secure  = True

        Response.Append cookie(obj cookieObject)

    End Sub

    Sub btnRemove cookie_OnClick(Sender As Object, E As EventArgs)

        obj cookieObject = New Http cookie( cookie_NAME)

        ' Expire it on the day I was born just so we're sure it's a date in the past.

        obj cookieObject.Expires = New DateTime(1974, 11, 12)

        Response.Append cookie(obj cookieObject)

    End Sub

    Sub btnGet cookie_OnClick(Sender As Object, E As EventArgs)

        obj cookieObject = Request. cookies( cookie_NAME)

        ' In Beta 1 this worked fine... not with Beta 2.

        'If Not(obj cookieObject = Nothing) Then

        If Not(obj cookieObject Is Nothing) Then

            lbl cookieDetails.Text        = obj cookieObject.Name

            lbl cookieDetailsName.Text    = obj cookieObject.Name

            lbl cookieDetailsValue.Text   = obj cookieObject.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 -> [email protected]

            lbl cookieDetailsExpires.Text = obj cookieObject.Expires.ToString

            lbl cookieDetailsDomain.Text  = obj cookieObject.Domain

            lbl cookieDetailsPath.Text    = obj cookieObject.Path

            lbl cookieDetailsSecure.Text  = obj cookieObject.Secure.ToString

            lbl cookieDetailsHasKeys.Text = obj cookieObject.HasKeys.ToString

        Else

            lbl cookieDetails.Text        = " cookie Not Set!"

            lbl cookieDetailsName.Text    = ""

            lbl cookieDetailsValue.Text   = ""

            lbl cookieDetailsExpires.Text = ""

            lbl cookieDetailsDomain.Text  = ""

            lbl cookieDetailsPath.Text    = ""

            lbl cookieDetailsSecure.Text  = ""

            lbl cookieDetailsHasKeys.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="btnSet cookie" text="Set cookie" OnClick="btnSet cookie_OnClick" runat="server" />

    <asp:Button type="submit" id="btnRemove cookie" text="Remove cookie" OnClick="btnRemove cookie_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="btnGet cookie" text="Get cookie Details" OnClick="btnGet cookie_OnClick" runat="server" />

</form>

<p>

<strong>Details of:</strong> <asp:label id="lbl cookieDetails" runat="server" />

</p>

<table >

    <thead>

        <tr>

            <th>Property</th>

            <th>Value</th>

        </tr>

    </thead>

    <tbody>

        <tr>

            <td>Name</td>

            <td><asp:label id="lbl cookieDetailsName" runat="server" /></td>

        </tr>

        <tr>

            <td>Value</td>

            <td><asp:label id="lbl cookieDetailsValue" runat="server" /></td>

        </tr>

        <tr>

            <td>Expires</td>

            <td><asp:label id="lbl cookieDetailsExpires" runat="server" /></td>

        </tr>

        <tr>

            <td>Domain</td>

            <td><asp:label id="lbl cookieDetailsDomain" runat="server" /></td>

        </tr>

        <tr>

            <td>Path</td>

            <td><asp:label id="lbl cookieDetailsPath" runat="server" /></td>

        </tr>

        <tr>

            <td>Secure</td>

            <td><asp:label id="lbl cookieDetailsSecure" runat="server" /></td>

        </tr>

        <tr>

            <td>Has Keys</td>

            <td><asp:label id="lbl cookieDetailsHasKeys" runat="server" /></td>

        </tr>

    </tbody>

</table>

</body>

</htm

vb.net cookie操作

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

继续阅读