为UIWebView请求设置Cookie

Tim*_*imm 5 uiwebview xamarin.ios nshttpcookie

我想在UIWebView我的MonoTouch应用程序中嵌入一​​个尚未实现的区域.

为了通过网站进行身份验证,我想设置一个包含当前会话密钥的cookie.

我尝试NSDictionary使用Cookie的属性创建一个,然后创建一个新的NSHttpCookie并将其添加到NSHttpCookieStorage.SharedStorage.

可悲的是,cookie似乎是空的,并没有用于请求.

如何构建具有属性的cookie以及评论这是否是最简单的方法的一个示例将非常感激.

pou*_*pou 7

根据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");
Run Code Online (Sandbox Code Playgroud)

您甚至可以NSHttpCookie从.NET 创建一个System.Net.Cookie.

注意:当API证明比它应该更复杂时,毫不犹豫地填写错误报告:-)


Anu*_*nuj 3

每当我需要将 cookie 和参数发送到服务器时,我都会使用 RestSharp 或 Hammock 之类的东西,然后将 response.Content 值传递到 UIWebView 的 loadHtmlString 方法中:

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

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("/"));
Run Code Online (Sandbox Code Playgroud)