• 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/
  • 相关阅读:
    vmware 连网问题
    js控制表单非法输入时提交
    简单的jsp&servlet 购物车项目
    html引入css不显示问题
    每天学点java_反射作用
    java选择特定的值2--抽象enum
    java选择特定的值
    1 小时 SQL 极速入门(一)
    460004600146002MNCMCCIMSI
    网络中存在2台DHCP服务器问题
  • 原文地址:https://www.cnblogs.com/waterBlog/p/3699630.html
Copyright © 2020-2023  润新知