Cookie Support
Using the CookieManager class you can:
- Retrieve cookies for a particular website from the browser
- Set the contents of a cookie in the browser
- Create a new cookie in the browser
- Delete a cookie in the browser
Retrieving Cookies
There are two functions that can be used to retrieve cookies from the browser:
C#
public CookieCollection GetCookies (string url)
public CookieCollection GetCookies (Uri uri)
Visual Basic
Public Function GetCookies(ByVal url As String) As System.Net.CookieCollection
Public Function GetCookies(ByVal uri As System.Uri) As System.Net.CookieCollection
These functions retrieve all the cookies from the browser for the specified website. For example:
Visual Basic
' Query the cookie
Creating and Setting Cookies
Below is an example of how to create or set a cookie. If the cookie already exists, it will be overwritten. If it does not exist, a new cookie will be created in the browser:
C#
// Let's create a new cookie for a url.
Visual Basic
' Let's create a new cookie for a url.
Deleting Cookies
Here is an example of how to delete a cookie:
C#
// Now delete the cookie.
ActiveBrowser.Cookies.DeleteCookie(siteCookies[0]);
Visual Basic
' Now delete the cookie.
ActiveBrowser.Cookies.DeleteCookie(siteCookies(0))
A simple foreach loop can be used to delete all the cookies for a particular website:
C#
// Purge any cookies associated with this server
System.Net.CookieCollection cookies = ActiveBrowser.Cookies.GetCookies(Settings.Current.BaseUrl);
foreach (System.Net.CookieCookie cookie in cookies)
{
ActiveBrowser.Cookies.DeleteCookie(cookie);
}
Visual Basic
' Purge any cookies associated with this server
Dim cookies As System.Net.CookieCollection = ActiveBrowser.Cookies.GetCookies(Settings.Current.BaseUrl)
For Each cookie As System.Net.Cookie In cookies
ActiveBrowser.Cookies.DeleteCookie(cookie)
Next cookie