在Silverlight應用程式中操作Cookie

2021-09-08 02:18:30 字數 2370 閱讀 6629

很多朋友來信問如何在silverlight 2中操作cookie,這裡專門寫篇文章介紹一下。為了實現在silverlight應用程式中對於cookie的操作,我們需要借助於htmlpage.document物件。

在使用htmlpage.document之前,請先新增system.windows.browser命名空間。本文介紹了如何在silverlight應用程式中操作cookie,並在最後給出了乙個操作cookie的公用類,大家可以直接在自己的應用程式中使用。

在silverlight 應用程式中,我們可以通過htmlpage.document.setproperty方法來設定cookie或者使用htmlpage.document物件的cookies屬性(後面會講到),如下**所示:

void btnset_click(object sender, routedeventargs e)

=;expires=",

this.txtkey.text,

this.txtvalue.text,

expir.tostring("r"));

htmlpage.document.setproperty("cookie", cookie);

}

這裡設定cookie的過期時間為一周,除了設定過期時間外還可以設定domain、path等,後面的幫助類中你將看到這一點。

如使用下面的介面寫入cookie:

我們可以通過htmlpage.document.getproperty方法來獲取所有cookie,另外在htmldocument中定義了cookies屬性,已經為我們封裝好了getproperty方法,可以直接使用,它的定義如下**所示:

public sealed class 

htmldocument : htmlobject

return

string.empty;

}set

this.setproperty("cookie", str);}}

}

如使用下面這段**來獲取乙個指定key的cookie值:

void btnretrieve_click(object sender, routedeventargs e)}}

}

如下圖所示:

刪除cookie非常簡單,清空cookie的值並設定它的過期時間,如下**所示:

void b***elete_click(object sender, routedeventargs e)

=;expires=",

this.txtkey.text, expir.tostring("r"));

htmlpage.document.setproperty("cookie", cookie);

}

1.寫入cookie

2.讀取cookie

3.刪除cookie

4.判斷cookie是否存在

當然如果你還有別的需求,可以再進一步完善,完整的**如下:

public class 

cookiesutils

public static void setcookie(string key, string value, timespan expires)

public static void setcookie(string key, string value, timespan? expires,

string path, string domain, bool secure)

if (!string.isnullorempty(path))

if (!string.isnullorempty(domain))

if (secure)

htmlpage.document.setproperty("cookie", cookie.tostring());

}public static string getcookie(string key)

public static void deletecookie(string key)

=;expires=",

key, expir.tostring("r"));

htmlpage.document.setproperty("cookie", cookie);

}public static bool exists(string key, string value)

=", key, value));}}

terrylee

出處:

協程函式應用

1.套模板 def init func res func args,kwargs next res return res def init func res func args,kwargs next res return res init eater init eater def eater na...

在Delphi開發的服務中呼叫指定應用程式

在很多時候,我們需要使用 服務啟動指定的應用程 序來做到隱蔽啟動程式的目的。但是當我們直接使用winexec來執行的時候,你會發現系統提示出錯。以下的 就是如何在 delphi編寫的服務中啟動指定的應用程式 function runprocess const processname string b...

Silverlight應用程式擴充套件服務

使用應用程式擴充套件服務可以擴充套件 silverlight 應用程式模型。通常使用擴充套件服務來封裝特定功能域中由多個應用程式使用的功能。例如,可以使用擴充套件服務來實現專用 處理或自定義資料訪問層。本主題包含以下各節 服務以您指定的順序進行註冊。這使您能夠建立對之前註冊的服務具有依賴項的服務。必...