Asp. Basic knowledge of cookie programming in net (5)

Source: Internet
Author: User
Tags allkeys count datetime integer tostring
asp.net|cookie| Programming Read Cookie Collection

The preceding example assumes that you want to read a Cookie with a known name. Sometimes, you may need to read all the cookies available to the page. To read the names and values of all the cookies available to the page, you can traverse the Request.Cookies collection using the following code:

Dim I as Integer
Dim Output as String = ""
Dim Acookie as HttpCookie
For i = 0 to Request.cookies.count-1
Acookie = Request.Cookies (i)
Output &= "Cookie name =" & Server.HTMLEncode (acookie.name) & "<br>"
Output &= "Cookie value =" & Server.HTMLEncode (Acookie.value) & _
& "<br><br>"
Next
Label1.Text = output

Note: When you run this code, you will most likely see a cookie,asp.net named "Asp.net_sessionid" with this Cookie to save your session's unique identifier. This session Cookie is not persisted to your hard disk. For more information about session cookies, see Cookies and Session state later in this article.
The preceding example has a limitation: if the Cookie has a subkey, the subkey is displayed with a separate name/value string. The HasKeys (English) property of the cookie can tell you whether the cookie has subkeys. If you have subkeys, you can drill down through the set of subkeys to get the names and values of each subkey.

As discussed earlier, you can get information about subkeys from the Cookie attribute Values (English), which is a collection of type NameValueCollection. You can read child key values directly from the values collection based on the index value. The corresponding subkey value can be obtained from the member AllKeys (English) of the Values collection, and the member returns a collection of strings.

The following example is a modification to the previous example. Example, the HasKeys property is used to test the subkey, and if a subkey is detected, the subkey is fetched from the Values collection:

Dim I as Integer
Dim J as Integer
Dim Output as String = ""
Dim Acookie as HttpCookie
Dim SubkeyName as String
Dim Subkeyvalue as String
For i = 0 to Request.cookies.count-1
Acookie = Request.Cookies (i)
Output &= "name =" & Acookie.name & "<br>"
If Acookie.haskeys Then
For j = 0 to Acookie.values.count-1
SubkeyName = Server.HTMLEncode (ACookie.Values.AllKeys (j))
Subkeyvalue = Server.HTMLEncode (Acookie.values (j))
Output &= "sub-key name =" & SubkeyName & "<br>"
Output &= "subkey value =" & Subkeyvalue & "<br><br>"
Next
Else
Output &= "value =" & Server.HTMLEncode (Acookie.value) & "<br><br>"
End If
Next
Label1.Text = output

You can also extract the key as a NameValueCollection object, as follows:

If Acookie.haskeys Then
Dim Cookievalues As _
System.Collections.Specialized.NameValueCollection = Acookie.values
Dim cookievaluenames () as String = Cookievalues.allkeys
For j = 0 to Cookievalues.count–1
SubkeyName = Server.HTMLEncode (Cookievaluenames (j))
Subkeyvalue = Server.HTMLEncode (Cookievalues (j))
Output &= "sub-key name =" & SubkeyName & "<br>"
Output &= "subkey value =" & Subkeyvalue & "<br><br>"
Next
Else
Output &= "value =" & Acookie.value & "<br><br>"
End If

Note: Remember that I called the Server.HTMLEncode method simply because I wanted to display the value of the Cookie on the page. If you are only testing the value of a Cookie, you do not have to encode it before you use it.

modifying and deleting cookies

Sometimes, you may need to modify a Cookie, change its value, or extend its validity period. (Keep in mind that you cannot read the expiration date of a Cookie because the browser does not pass the expiration information to the server.) )

Of course, you don't actually change cookies directly. Although you can get and manipulate cookies from the Request.Cookies collection, the cookie itself still exists somewhere on the user's hard disk. Therefore, modifying a cookie actually means creating a new cookie with a new value and sending the cookie to the browser to overwrite the old cookie on the client.

The following example shows how to change the value of a Cookie used to store site access times:

Dim Counter as Integer
If request.cookies ("counter") is nothing Then
Counter = 0
Else
Counter = CInt (Request.Cookies ("counter"). Value)
End If
Counter + 1
Response.Cookies ("Counter"). Value = counter. Tostring
Response.Cookies ("Counter"). Expires = DateTime.Now.AddDays (1)

Or:

Dim Ctrcookie as HttpCookie
Dim Counter as Integer
If request.cookies ("counter") is nothing Then
Ctrcookie = New HttpCookie ("counter")
Else
Ctrcookie = Request.Cookies ("Counter")
End If
Counter = CInt (ctrcookie.value) + 1
Ctrcookie.value = counter. Tostring
Ctrcookie.expires = DateTime.Now.AddDays (1)
RESPONSE.COOKIES.ADD (Ctrcookie)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.