HTTP Cookies

From PowerUI
Jump to: navigation, search

Whenever a website creates one or more cookies, they will be stored into a CookieJar (a C# class). Importantly, they are not persistent by default. PowerUI is, after all, a UI framework. Cookie jars are stored in memory and there's one per host. In order to obtain the cookie jar for a particular host:

// string host="powerui.kulestar.com";

PowerUI.Http.CookieJar jar = PowerUI.Http.CookieJar.Get(host);

// jar is null if the host has set no cookies.
// It may be for a parent host (e.g kulestar.com) if no more specific one is found.

Cookie Jars and persistence

Cookie jars have a ToString() method which will format all the cookies it holds into a storable cookie header. Save that string however you want, e.g. in PlayerPrefs. Reloading the jar is a little harder but can be roughly done like so:


// string host;
// string cookies;

// Create the jar:
PowerUI.Http.CookieJar jar = PowerUI.Http.CookieJar.Get(host,true);

// Get all the cookies:
string[] allCookies = cookies.Split(';');

// Add each one:
foreach(string cookie in allCookies){

    // Add it:
    jar.Add(new Cookie(cookie));

}