' Create user id and password cookies ' set their values and add to the collection Dim cookie As HttpCookie = New HttpCookie("UID") cookie.Value = "myid" cookie.Expires = #9/28/2002# Response.Cookies.Add(cookie) cookie = New HttpCookie("PASS") cookie.Value = "mypass" cookie.Expires = #9/28/2002# Response.Cookies.Add(cookie)You can read cookies using the Request.Cookies property. The code listed in Listing 2 reeds the cookies from the browser and adds them to a ListBox control.Listing 2. Reading cookies ' Read cookies Dim cookieCols As New HttpCookieCollection() cookieCols = Request.Cookies Dim str As String ' Read and add all cookies to the list box For Each str In cookieCols ListBox1.Items.Add("Cookie: " + str) ListBox1.Items.Add("Value:" & _ Request.Cookies(str).Value) Next You can use the Remove and Clear methods of HttpCookieCollection to remove a particular cookie by name or all cookies respectively. The code listed in Listing 3 delete the cookies using the Remove method.Listing 3. Deleting cookies Dim cookieCols As New HttpCookieCollection() cookieCols = Request.Cookies Dim str As String ' Read and add all cookies to the list box Request.Cookies.Remove("PASS") Request.Cookies.Remove("UID")A cookie can also store multiple values. This type of cookies called cookie dictionaries. You use Values property to create and read this type of cookies. The code listed in Listing 4 creates a dictionary cookie.Listing 4. Creating a dictionary cookieDim cookDict As HttpCookie = New HttpCookie("dict") cookDict.Values("fname") = "first name" cookDict.Values("lname") = "last name" cookDict.Values("Address") = "address" Response.Cookies.Add(cookDict)
' set their values and add to the collection
Dim cookie As HttpCookie = New HttpCookie("UID")
cookie.Value = "myid"
cookie.Expires = #9/28/2002#
Response.Cookies.Add(cookie)
cookie = New HttpCookie("PASS")
cookie.Value = "mypass"
cookie.Expires = #9/28/2002#
Response.Cookies.Add(cookie)You can read cookies using the Request.Cookies property. The code listed in Listing 2 reeds the cookies from the browser and adds them to a ListBox control.Listing 2. Reading cookies ' Read cookies
Dim cookieCols As New HttpCookieCollection()
cookieCols = Request.Cookies
Dim str As String
' Read and add all cookies to the list box
For Each str In cookieCols
ListBox1.Items.Add("Cookie: " + str)
ListBox1.Items.Add("Value:" & _
Request.Cookies(str).Value)
Next You can use the Remove and Clear methods of HttpCookieCollection to remove a particular cookie by name or all cookies respectively. The code listed in Listing 3 delete the cookies using the Remove method.Listing 3. Deleting cookies Dim cookieCols As New HttpCookieCollection()
cookieCols = Request.Cookies
Dim str As String
' Read and add all cookies to the list box
Request.Cookies.Remove("PASS")
Request.Cookies.Remove("UID")A cookie can also store multiple values. This type of cookies called cookie dictionaries. You use Values property to create and read this type of cookies. The code listed in Listing 4 creates a dictionary cookie.Listing 4. Creating a dictionary cookieDim cookDict As HttpCookie = New HttpCookie("dict")
cookDict.Values("fname") = "first name"
cookDict.Values("lname") = "last name"
cookDict.Values("Address") = "address"
Response.Cookies.Add(cookDict)
Response.Cookies("结构名").Expires="yyyy-MM-dd"
HttpCookie cookie = New HttpCookie("UID")
cookie.Value = "myname"
cookie.Expires = dt;
/////////////////////
判断用户是否登陆,最好用Session来判断