if not request.cookies("username") is nothing then label1.text = server.htmlencode(request.cookies("username").value) end if
if not request.cookies("username") is nothing then dim acookie as httpcookie = request.cookies("username") label1.text = server.htmlencode(acookie.value) end if
注意:由于不同的瀏覽器保存 cookie 的方式也不同,所以同一臺計算機上的不同瀏覽器不一定能夠相互讀取各自的 cookie。例如,如果使用 internet explorer 測試一個頁面,然后再使用其他瀏覽器進行測試,那么后者就不會找到 internet explorer 保存的 cookie。當然,大多數人一般都是使用同一種瀏覽器進行 web 交互的,因此在大多數情況下不會出現問題。但有時還是會遇到問題,比如您要測試應用程序對瀏覽器的兼容性。
讀取 cookie 中子鍵值的方法與設置該值的方法類似。以下是獲取子鍵值的一種方法:
if not request.cookies("userinfo") is nothing then label1.text = _ server.htmlencode(request.cookies("userinfo")("username")) label2.text = _ server.htmlencode(request.cookies("userinfo")("lastvisit")) end if
if not request.cookies("userinfo") is nothing then dim userinfocookiecollection as _ system.collections.specialized.namevaluecollection userinfocookiecollection = request.cookies("userinfo").values label1.text = server.htmlencode(userinfocookiecollection("username")) label2.text = server.htmlencode(userinfocookiecollection("lastvisit")) end if