我想在我的MonoTouch应用程序中嵌入一个UIWebView
,用于尚未本机实现的区域。
为了通过网站进行身份验证,我想设置一个包含当前会话密钥的cookie。
我尝试使用Cookie的属性创建NSDictionary
,然后创建新的NSHttpCookie
并将其添加到NSHttpCookieStorage.SharedStorage
。
可悲的是,cookie似乎是空的,并没有用于请求。
如何构建具有属性的cookie以及评论这是否是最简单的方法的一个示例将非常感激。
答案 0 :(得分:7)
关注Anuj的bug report我对创建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");
您甚至可以从.NET NSHttpCookie
创建System.Net.Cookie
。
注意:当API被证明比它应该更复杂时,毫不犹豫地填写错误报告: - )
答案 1 :(得分:3)
每当我需要将cookie和params发送到服务器时,我使用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));
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("/"));
答案 2 :(得分:2)
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/
上发布错误报告亚历
答案 3 :(得分:0)
我已经编写了NSMutableURLRequest + XSURLRequest catagory和XSCookie类来执行此操作;-) http://blog.peakji.com/cocoansurlrequest-with-cookies/
答案 4 :(得分:0)
这可能会让你领先。以前我用过类似的策略来制作
WebRequest到站点并收集存储在.Net / Mono CookieStore中的cookie。然后在UIWebView中加载URL时,我将这些cookie复制到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));
}
}