【问题标题】:NSHttpCookie getting cookies in case of iOS version 10NSHttpCookie 在 iOS 版本 10 的情况下获取 cookie
【发布时间】:2018-11-05 10:04:34
【问题描述】:

我想在 Xamain iOS 中使用 WebClient 实现下载 pdf 文档功能。目前它非常适合 iOS 11,我正在使用 HttpCookieStore 获取 cookie。 对于 iOS 10,我无法检索 cookie。

处理此问题的流程是 1. 获取 cookie 2. 创建 Web 客户端服务器请求,并将 cookie 添加到标头中。

对于 iOS 11, 以下是获取 cookie 的语法:

NSHttpCookie[] cookieStoreData;

    if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
            {
                var cookieStore = webView.Configuration.WebsiteDataStore.HttpCookieStore;
                cookieStoreData = await cookieStore.GetAllCookiesAsync();
            }

对于 iOS 10,

cookieStoreData = NSHttpCookieStorage.SharedStorage.Cookies;  

在这些情况下它不会返回任何 cookie,因此下一次调用 webclient 到服务器会失败(在 iOS 10 的情况下)。

StringBuilder cookies = new StringBuilder();
var webClient = new WebClient ();

foreach (var temp in cookieStoreData)
{
  cookies.Append(temp.Name + "=" + temp.Value + ";");
}  

webClient.Headers.Add(HttpRequestHeader.Cookie, cookies.ToString()); 



webClient.DownloadDataAsync (webView.Url);
webClient.DownloadDataCompleted += (s, e) => {
if (e.Error == null)
{
    // Downloading data......
}
else 
{
    Console.WriteLine("URL for page : " + e.Error.GetType());
    Console.WriteLine("URL for page : " + e.Error);
    new UIAlertView("Done", "Download Failed. Please try again.", null, "OK", null).Show();
}

【问题讨论】:

  • 你想从哪里获取cookies?在UIWebview、WKWebview或者一些URL请求比如AFNetworking?
  • @LucasZhang-MSFT 这是 WKWebView。现在我可以使用决定策略方法检索 cookie,但与 HttpCookieStore 相比,我无法获得完整的 cookie 列表
  • 获取 cookie:[Export("webView:decidePolicyForNavigationResponse:decisionHandler:")] public void DecidePolicy(WKWebView webView, WKNavigationResponse navigationResponse, Action decisionHandler) { NSHttpUrlResponse response; response = (NSHttpUrlResponse)navigationResponse.Response; cookies_holder = NSHttpCookie.CookiesWithResponseHeaderFields(response.AllHeaderFields, webView.Url);决策处理程序(WKNavigationResponsePolicy.Allow); }

标签: xamarin cookies xamarin.ios uiwebview webclient


【解决方案1】:

原因:

  1. NSHTTPCookieStorage 不可靠,因为 WKWebView 完成了所有 网络在一个单独的过程中,所以你无法获得 Web 视图使用的 NSHTTPCookieStorage 对象。
  2. JavaScript(如 WKUserScript ) 没有 查看任何带有 HttpOnly 标记的 cookie。
  3. WKWebsiteDataStore 让您知道 cookie 的存在,但 不让您获取内容。
  4. 您展示的委托方法不会看到所有 cookie,因为 并非所有响应都是导航响应。

解决方案: 最好的解决方案是使用UIWebview 而不是WKWebView。更多详细信息,您可以访问here

【讨论】:

  • 我看到你问了一个类似的问题。stackoverflow.com/questions/53172888/…。如果我的回答有用,你可以删除它
  • 会在分析后更新这篇文章。再次感谢您的回答!
  • 嗨@LucasZhang-MSFT,UIWebview 已经死了。自 Apple 宣布以来,我们无法发布我们的应用程序。 Wkwebview 现在有你知道的解决方案吗?
  • @Erdogan 我也面临同样的问题。如果您有任何解决方案,请帮助我。
  • @YogendraPatel 是的。如果您使用特定网站,您就会知道正在使用哪些 cookie。我将它们设置在 OnElementChanged 中,这是一种欺骗方式,但有效。 NSHttpCookieStorage.SharedStorage.AcceptPolicy = NSHttpCookieAcceptPolicy.Always; NSHttpCookie cook1 = new NSHttpCookie("JSESSIONPR", "/VROnline", "www...com"); SHttpCookieStorage.SharedStorage.SetCookie(cook1);您可以同时添加太多 cookie。希望这对您有所帮助。
猜你喜欢
  • 1970-01-01
  • 2013-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-06
相关资源
最近更新 更多