zoukankan      html  css  js  c++  java
  • Silver 操作Cookie

    public class CookiesUtils
    {
        public static void SetCookie(String key, String value)
        {
            SetCookie(key, value, null, null, null, false);
        }
        public static void SetCookie(String key, String value, TimeSpan expires)
        {
            SetCookie(key, value, expires, null, null, false);
        }
        public static void SetCookie(String key, String value, TimeSpan? expires,
            String path, String domain, bool secure)
        {
            StringBuilder cookie = new StringBuilder();
            cookie.Append(String.Concat(key, "=", value));
            if (expires.HasValue)
            {
                DateTime expire = DateTime.UtcNow + expires.Value;
                cookie.Append(String.Concat(";expires=", expire.ToString("R")));
            }
            if (!String.IsNullOrEmpty(path))
            {
                cookie.Append(String.Concat(";path=", path));
            }
            if (!String.IsNullOrEmpty(domain))
            {
                cookie.Append(String.Concat(";domain=", domain));
            }
            if (secure)
            {
                cookie.Append(";secure");
            }
            HtmlPage.Document.SetProperty("cookie", cookie.ToString()); 
        }
        public static string GetCookie(String key)
        {
            String[] cookies = HtmlPage.Document.Cookies.Split(';');
            String result = (from c in cookies
                            let keyValues = c.Split('=')
                            where keyValues.Length == 2 && keyValues[0].Trim() == key.Trim()
                            select keyValues[1]).FirstOrDefault();
            return result;
        }
        public static void DeleteCookie(String key)
        {
            DateTime expir = DateTime.UtcNow - TimeSpan.FromDays(1);
            string cookie = String.Format("{0}=;expires={1}",
                key, expir.ToString("R"));
            HtmlPage.Document.SetProperty("cookie", cookie);
        }
        public static bool Exists(String key, String value)
        {
            return HtmlPage.Document.Cookies.Contains(String.Format("{0}={1}", key, value));
        }
    }

    摘自于:http://kb.cnblogs.com/page/42901/3/
  • 相关阅读:
    一个简单的makefile,一次性编译本文件夹下所有的cpp文件
    c++ 最短路两种算法
    C++语言十进制数,CDecimal(未完成)
    C语言面向对象的简便方法
    C语言2048
    C图书借还示例
    Javascript 备忘
    原型与原型链
    css3动画-跳动圈
    学习css3动画
  • 原文地址:https://www.cnblogs.com/waterBlog/p/3699630.html
Copyright © 2011-2022 走看看