我正在使用 Monotouch,并尝试将 NsHttpCookie 对象存储在我的模拟器的共享存储中,以用于我未来的请求。我正在使用NSHttpCookieStorage.SharedStorage.SetCookie (NSHttpCookie)
方法。
但是,在我这样做之后,当我尝试检索 cookie(通过NSHttpCookieStorage.SharedStorage.Cookies
)时,我看到 0 个 cookie。
更详细地说 - 我正在将系统 cookie 转换为 NSHttpCookie。在那之后,我试着用
NSHttpCookieStorage.SharedStorage.AcceptPolicy = NSHttpCookieAcceptPolicy.Always;
NSHttpCookieStorage.SharedStorage.SetCookie (
convertSystemCookieToNsCookie(resp.Cookies[0]));
当我尝试用
NSHttpCookie[] cookies = NSHttpCookieStorage.SharedStorage.Cookies;
Console.Out.WriteLine ("Found cookies " + cookies.Length);
它说 0 cookie。我在任何地方都没有看到任何异常。这是 convertSystemCookieToNsCookie 方法 -
private NSHttpCookie convertSystemCookieToNsCookie(Cookie cookie) {
var properties = new NSMutableDictionary ();
properties.Add (NSHttpCookie.KeyOriginURL, new NSString(cookie.Domain));
properties.Add (NSHttpCookie.KeyName, new NSString(cookie.Name));
properties.Add (NSHttpCookie.KeyValue, new NSString(cookie.Value));
properties.Add (NSHttpCookie.KeyPath, new NSString (cookie.Path));
NSDate cookieDate = NSDate.FromTimeIntervalSinceReferenceDate ((
cookie.Expires - (new DateTime (2001, 1, 1, 0, 0, 0))).TotalSeconds);
properties.Add (NSHttpCookie.KeyExpires, cookieDate);
return new NSHttpCookie (properties);
}
知道我做错了什么吗?非常感谢!