Set Cookie for UIWebView Requests

I want to insert UIWebViewMonoTouch into my application for an area that is not yet implemented initially.

In order to authenticate from the website, I want to set a cookie containing the key for the current session.

I tried to create NSDictionarywith properties for the Cookie, and then create a new one NSHttpCookieand add it to NSHttpCookieStorage.SharedStorage.

Unfortunately, the cookie seems empty and is not used for the request.

An indication of how to create a cookie with properties and a comment about whether this is the easiest way to do this would be greatly appreciated.

+5
source share
5 answers

, cookie , - RestSharp Hammock, . UIWebView loadHtmlString:

//setup cookies and params here
var response = client.Execute(req);
_webView = new UIWebView();
_webView.LoadHtmlString(response.Content, new NSUrl(baseUrl));

API NSDictionary API :

var props = new NSMutableDictionary ();
props.Add (NSHttpCookie.KeyOriginURL, new
NSString("http://yodawg.com"));
props.Add (NSHttpCookie.KeyName, new NSString("iherd"));
props.Add (NSHttpCookie.KeyValue, new NSString("ulikecookies"));
props.Add (NSHttpCookie.KeyPath, new NSString("/"));
+3

Anuj , cookie. , MonoTouch NSHttpCookie, System.Net.Cookie, - :

// this ctor requires all mandatory parameters 
// so you don't have to guess them while coding
var cookie = new NSHttpCookie ("iherd", "ulikecookies", "/", "yodawg.com");

NSHttpCookie .NET System.Net.Cookie.

. , API , : -)

+7

AFAIK cookie, UIWebView

        NSHttpCookie cookie = new NSHttpCookie()
        {
            Domain = "yourdomain.com",
            Name = "YourName",
            Value = "YourValue" //and any other info you need to set
        };
        NSHttpCookieStorage cookiejar = NSHttpCookieStorage.SharedStorage;
        cookiejar.SetCookie(cookie);

MAC, , ,


, , , ,

var objects = new object[] { "http://yoururl.com", "CookieName", "CookieValue", "/" };
var keys = new object[] { "NSHTTPCookieOriginURL", "NSHTTPCookieName", "NSHTTPCookieValue", "NSHTTPCookiePath" };
NSDictionary properties = (NSDictionary) NSDictionary.FromObjectsAndKeys(objects, keys);
NSHttpCookie cookie = NSHttpCookie.CookieFromProperties(properties);
NSHttpCookieStorage.SharedStorage.SetCookie(cookie);

, , , monotouch, ,

var objects = new object[] { "http://yoururl.com", "CookieName", "CookieValue", "/" };
var keys = new object[] { "NSHTTPCookieOriginURL", "NSHTTPCookieName", "NSHTTPCookieValue", "NSHTTPCookiePath" };
NSDictionary properties = (NSDictionary) NSDictionary.FromObjectsAndKeys(objects, keys);
NSHttpCookie cookie = (NSHttpCookie) Runtime.GetNSObject(Messaging.IntPtr_objc_msgSend_IntPtr(new Class("NSHTTPCookie").Handle, new Selector("cookieWithProperties:").Handle, properties.Handle))
NSHttpCookieStorage.SharedStorage.SetCookie(cookie);

using MonoTouch.ObjCRuntime;,

, , https://bugzilla.xamarin.com/

+2

For this, I wrote the NSMutableURLRequest + XSURLRequest category and the XSCookie class ;-) http://blog.peakji.com/cocoansurlrequest-with-cookies/

0
source

This may give you an edge. I used to use a similar strategy to make

WebRequest to the site and collect cookies stored in the .Net / Mono CookieStore. Then, loading the URL into the UIWebView, I copied these cookies to NSHttpCookieStorage.

public NSHttpCookieStorage _cookieStorage; 

    /// <summary>
    /// Convert the .NET cookie storage to the iOS NSHttpCookieStorage with Login Cookies
    /// </summary>
    void DotNetCookieStoreToNSHttpCookieStore()
    {
        foreach (Cookie c in _cookies.GetCookies(new Uri(UrlCollection["Login"], UriKind.Absolute))) {
            Console.WriteLine (c);
            _cookieStorage.SetCookie(new NSHttpCookie(c));
        }
    }
0
source

All Articles