熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

在Silverlight應用程序中操作Cookie

2013-11-13 10:13:49  來源: .NET編程 

  概述

  很多朋友來信問如何在Silverlight 中操作Cookie這裡專門寫篇文章介紹一下為了實現在Silverlight應用程序中對於Cookie的操作我們需要借助於HtmlPageDocument對象

  在使用HtmlPageDocument之前請先添加SystemWindowsBrowser命名空間本文介紹了如何在Silverlight應用程序中操作Cookie並在最後給出了一個操作Cookie的公用類大家可以直接在自己的應用程序中使用

  寫入Cookie

  在Silverlight 應用程序中我們可以通過HtmlPageDocumentSetProperty方法來設置Cookie或者使用HtmlPageDocument對象的Cookies屬性(後面會講到)如下代碼所示          void btnSet_Click(object sender RoutedEventArgs e)
        {
            DateTime expir = DateTimeUtcNow + TimeSpanFromDays();
            String cookie = StringFormat({}={};expires={}
            thistxtKeyText
            thistxtValueText
            expirToString(R));
            HtmlPageDocumentSetProperty(cookie cookie);
        }

  這裡設置Cookie的過期時間為一周除了設置過期時間外還可以設置domainpath等後面的幫助類中你將看到這一點

  如使用下面的界面寫入Cookie

  TerryLee_0146

  讀取Cookie

  我們可以通過HtmlPageDocumentGetProperty方法來獲取所有Cookie另外在HtmlDocument中定義了Cookies屬性已經為我們封裝好了GetProperty方法可以直接使用它的定義如下代碼所示          public sealed class HtmlDocument : HtmlObject
        {
            public string Cookies
            {
                get{
                    HtmlPageVerifyThread();
                    String property = thisGetProperty(cookie) as String;
                    if (property != null)
                    {
                        return property;
                    }
                    return StringEmpty;
                }
                set{
                    HtmlPageVerifyThread();
                    String str = value;
                    if (StringIsNullOrEmpty(str))
                    {
                        str = stringEmpty;
                    }
                    thisSetProperty(cookie str);
                }
            }
        }

  如使用下面這段代碼來獲取一個指定Key的Cookie值          void btnRetrieve_Click(object sender RoutedEventArgs e)
        {
            String[] cookies = HtmlPageDocumentCookiesSplit(;);
            foreach (String cookie in cookies)
            {
                String[] keyValues = cookieSplit(=);
                if (keyValuesLength == )
                {
                    if (keyValues[]Trim() == thistxtKeyTextTrim())
                    {
                        thistxtValueText = keyValues[];
                    }
                }
            }
        }

  如下圖所示

  TerryLee_0147

  刪除Cookie

  刪除Cookie非常簡單清空Cookie的值並設置它的過期時間如下代碼所示          void btnDelete_Click(object sender RoutedEventArgs e)
        {
            DateTime expir = DateTimeUtcNow TimeSpanFromDays();
            string cookie = StringFormat({}=;expires={}
                thistxtKeyText expirToString(R));
            HtmlPageDocumentSetProperty(cookie cookie);
        }

  Cookie幫助類

  由於在開發中我們可能會經常用到對於Cookie的操作我在這裡總結了一個簡單的Silverlight中操作Cookie幫助類大家可以直接在自己的項目中使用主要有如下幾個功能

  寫入Cookie

  讀取Cookie

  刪除Cookie

  判斷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();
                cookieAppend(StringConcat(key = value));
                if (expiresHasValue)
                {
                    DateTime expire = DateTimeUtcNow + expiresValue;
                    cookieAppend(StringConcat(;expires= expireToString(R)));
                }
                if (!StringIsNullOrEmpty(path))
                {
                    cookieAppend(StringConcat(;path= path));
                }
                if (!StringIsNullOrEmpty(domain))
                {
                    cookieAppend(StringConcat(;domain= domain));
                }
                if (secure)
                {
                    cookieAppend(;secure);
                }
                HtmlPageDocumentSetProperty(cookie cookieToString());
            }
            public static string GetCookie(String key)
            {
                String[] cookies = HtmlPageDocumentCookiesSplit(;);
                String result = (from c in cookies
                                let keyValues = cSplit(=)
                                where keyValuesLength == && keyValues[]Trim() == keyTrim()
                                select keyValues[])FirstOrDefault();
                return result;
            }
            public static void DeleteCookie(String key)
            {
                DateTime expir = DateTimeUtcNow TimeSpanFromDays();
                string cookie = StringFormat({}=;expires={}
                    key expirToString(R));
                HtmlPageDocumentSetProperty(cookie cookie);
            }
            public static bool Exists(String key String value)
            {
                return HtmlPageDocumentCookiesContains(StringFormat({}={} key value));
            }
        }


From:http://tw.wingwit.com/Article/program/net/201311/12979.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.