为 UIWebView 请求设置 Cookie [英] Set Cookie for UIWebView requests
问题描述
我想将 UIWebView
嵌入到我的 MonoTouch 应用程序中,用于尚未本地实现的区域.
I want to embed an UIWebView
into my MonoTouch application for an area that is not yet implemented natively.
为了向网站进行身份验证,我想设置一个包含当前会话密钥的 cookie.
In order to authenticate with the website I want to set a cookie containing a key for the current session.
我尝试使用 Cookie 的属性创建一个 NSDictionary
,然后创建一个新的 NSHttpCookie
并将其添加到 NSHttpCookieStorage.SharedStorage
.
I tried creating a NSDictionary
with the properties for the Cookie and then create a new NSHttpCookie
and add it to the NSHttpCookieStorage.SharedStorage
.
遗憾的是,cookie 似乎是空的,没有用于请求.
Sadly the cookie seems to be empty and not used for the request.
非常感谢如何使用属性构建 cookie 的示例,以及关于这是否是最简单的方法的评论.
An example of how to build be cookie with properties and a comment on whether or not this is the simplest way to do this would be greatly appreciated.
推荐答案
每当我需要将 cookie 和参数发送到服务器时,我都会使用 RestSharp 或 Hammock 之类的东西,然后将 response.Content 值传递给 UIWebView 的 loadHtmlString 方法:
Whenever I need to send cookies and params up to the server I use something like RestSharp or Hammock and then pass the response.Content value into UIWebView's loadHtmlString method:
//setup cookies and params here
var response = client.Execute(req);
_webView = new UIWebView();
_webView.LoadHtmlString(response.Content, new NSUrl(baseUrl));
NSDictionary API 也相当简单:
The NSDictionary API is fairly trivial too:
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("/"));
这篇关于为 UIWebView 请求设置 Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!