色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

在Silverlight應(yīng)用程序中操作Cookie

概述

很多朋友來(lái)信問(wèn)如何在Silverlight 2中操作Cookie,這里專門寫篇文章介紹一下。為了實(shí)現(xiàn)在Silverlight應(yīng)用程序中對(duì)于Cookie的操作,我們需要借助于HtmlPage.Document對(duì)象。

在使用HtmlPage.Document之前,請(qǐng)先添加System.Windows.Browser命名空間。本文介紹了如何在Silverlight應(yīng)用程序中操作Cookie,并在最后給出了一個(gè)操作Cookie的公用類,大家可以直接在自己的應(yīng)用程序中使用。

寫入Cookie

在Silverlight 應(yīng)用程序中,我們可以通過(guò)HtmlPage.Document.SetProperty方法來(lái)設(shè)置Cookie或者使用HtmlPage.Document對(duì)象的Cookies屬性(后面會(huì)講到),如下代碼所示:

void btnSet_Click(object sender, RoutedEventArgs e){    DateTime expir = DateTime.UtcNow + TimeSpan.FromDays(7);    String cookie = String.Format("{0}={1};expires={2}",    this.txtKey.Text,    this.txtValue.Text,     expir.ToString("R"));    HtmlPage.Document.SetProperty("cookie", cookie);}

這里設(shè)置Cookie的過(guò)期時(shí)間為一周,除了設(shè)置過(guò)期時(shí)間外還可以設(shè)置domain、path等,后面的幫助類中你將看到這一點(diǎn)。

如使用下面的界面寫入Cookie:

TerryLee_0146 

讀取Cookie

我們可以通過(guò)HtmlPage.Document.GetProperty方法來(lái)獲取所有Cookie,另外在HtmlDocument中定義了Cookies屬性,已經(jīng)為我們封裝好了GetProperty方法,可以直接使用,它的定義如下代碼所示:

public sealed class HtmlDocument : HtmlObject{    public string Cookies    {        get{            HtmlPage.VerifyThread();            String property = this.GetProperty("cookie") as String;            if (property != null)            {                return property;            }            return String.Empty;        }        set{            HtmlPage.VerifyThread();            String str = value;            if (String.IsNullOrEmpty(str))            {                str = string.Empty;            }            this.SetProperty("cookie", str);        }    }}

如使用下面這段代碼來(lái)獲取一個(gè)指定Key的Cookie值:

void btnRetrieve_Click(object sender, RoutedEventArgs e){    String[] cookies = HtmlPage.Document.Cookies.Split(';');    foreach (String cookie in cookies)    {        String[] keyValues = cookie.Split('=');        if (keyValues.Length == 2)        {            if (keyValues[0].Trim() == this.txtKey.Text.Trim())            {                this.txtValue.Text = keyValues[1];            }        }    }}

如下圖所示:

TerryLee_0147

刪除Cookie

刪除Cookie非常簡(jiǎn)單,清空Cookie的值并設(shè)置它的過(guò)期時(shí)間,如下代碼所示:

void btnDelete_Click(object sender, RoutedEventArgs e){    DateTime expir = DateTime.UtcNow - TimeSpan.FromDays(1);    string cookie = String.Format("{0}=;expires={1}",        this.txtKey.Text, expir.ToString("R"));    HtmlPage.Document.SetProperty("cookie", cookie);}

Cookie幫助類

由于在開發(fā)中,我們可能會(huì)經(jīng)常用到對(duì)于Cookie的操作,我在這里總結(jié)了一個(gè)簡(jiǎn)單的Silverlight中操作Cookie幫助類,大家可以直接在自己的項(xiàng)目中使用,主要有如下幾個(gè)功能:

1.寫入Cookie

2.讀取Cookie

3.刪除Cookie

4.判斷Cookie是否存在

當(dāng)然如果你還有別的需求,可以再進(jìn)一步完善,完整的代碼如下:

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));    }}

總結(jié)

本文介紹了在Silverlight應(yīng)用程序中如何操作Cookie,希望對(duì)大家有所幫助。

NET技術(shù)在Silverlight應(yīng)用程序中操作Cookie,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 西充县| 孟津县| 申扎县| 内江市| 类乌齐县| 深泽县| 桐柏县| 平果县| 福建省| 台东市| 崇仁县| 会昌县| 永和县| 乌海市| 和田市| 保山市| 抚州市| 长岭县| 三原县| 临澧县| 桂东县| 肇东市| 屏东县| 天长市| 广汉市| 互助| 五原县| 拉萨市| 突泉县| 开鲁县| 康平县| 汝州市| 南川市| 自治县| 广平县| 米泉市| 翁源县| 焉耆| 穆棱市| 嘉兴市| 永济市|