精彩专题推荐:建站之入门课 建站之必修课 建站之关键课 网站价值所在 流量提高专题 css+div 标准 个人网站打造全过程
返回建站学首页
导航:
建站首页 | 网站设计 | 网站开发 | 网站运营 | 网页软件 | 建站指南 | 搜索优化 | 图像处理 | 视频教程 | 书籍教程 | 建站专题
当前位置:首页>网站开发>asp.net教程>正文

Asp.Net中对Cookie的基本操作


来源:不详 时间:07-09-03 点击: 点击这里收藏本文

实例代码演示Asp.Net中对Cookie的基本操作。

Imports System.Web.HttpContext
Public Class CookieFramework
    '写入单个Cookie
    Public Shared Function WriteCookie(ByVal CookieName As String, ByVal CookieValue As String, ByVal ExpiresDate As Integer) As Boolean
        Dim aCookie As New HttpCookie(CookieName)
        aCookie.Value = CookieValue
        aCookie.Expires = DateTime.Now.AddDays(ExpiresDate)
        System.Web.HttpContext.Current.Response.Cookies.Add(aCookie)
    End Function

    '给Cookie集合添加子项
    Public Shared Function WriteCookies(ByVal CookieName As String, ByVal CookieItem As String, ByVal ItemValue As String, ByVal ExpiresDate As Integer) As Boolean
        Dim aCookie As HttpCookie
        If Current.Request.Cookies(CookieName) Is Nothing Then
            aCookie = New HttpCookie(CookieName)
        Else
            aCookie = Current.Request.Cookies(CookieName)
        End If
        aCookie.Values(CookieItem) = ItemValue
        aCookie.Expires = DateTime.Now.AddDays(ExpiresDate)
        System.Web.HttpContext.Current.Response.Cookies.Add(aCookie)
    End Function
    '读取单个Cookie
    Public Shared Function ReadCookie(ByVal CookieName As String) As String
        If Current.Request.Cookies(CookieName) Is Nothing Then
            Return Nothing
        Else
            Return Current.Request.Cookies(CookieName).Value
        End If
    End Function

    '读取Cookie集合中的子项内容
    Public Shared Function ReadCookies(ByVal CookieName As String, ByVal CookieItem As String) As String
        If Current.Request.Cookies(CookieName) Is Nothing Then
            Return Nothing
        Else
            If Current.Request.Cookies(CookieName).Values(CookieItem) Is Nothing Then
                Return Nothing
            Else
                Return Current.Request.Cookies(CookieName).Values(CookieItem)
            End If
        End If
    End Function
    '删除整个Cookie
    Public Shared Function DeleteCookie(ByVal CookieName As String) As Boolean
        Dim aCookie As New HttpCookie(CookieName)
        Dim i As Integer
        Dim limit As Integer = Current.Request.Cookies.Count - 1
        For i = 0 To limit
            aCookie = Current.Request.Cookies(i)
            aCookie.Expires = DateTime.Now.AddDays(-1)
            Current.Response.Cookies.Add(aCookie)
        Next
    End Function
    '删除Cookie集合中的子项
    Public Shared Function DeleteCookies(ByVal CookieName As String, ByVal ItemName As String) As Boolean
        Dim aCookie As HttpCookie = Current.Request.Cookies(CookieName)
        aCookie.Values.Remove(ItemName)
        aCookie.Expires = DateTime.Now.AddDays(1)
        Current.Response.Cookies.Add(aCookie)
    End Function
End Class


  把此文章收藏到:          
广而告之
文章搜索
  • Google JZxue.Com

关于我们 | 联系我们 | 友情链接 | 网站地图
Copyright © 2005 - 2006 建站学 All rights reserved.