【问题标题】:Can't get cookie with HttpClient and windows phone 8无法使用 HttpClient 和 windows phone 8 获取 cookie
【发布时间】:2014-02-13 03:00:33
【问题描述】:

我必须进行表单身份验证才能使用服务,但我无法获取 cookie。一种解决方案是每次做两个请求,一个是登录页面,一个是我想要获取的资源。我在 wpf 和控制台应用程序上做了同样的事情,它运行良好,但在 windows phone 8 中却不行。这是我的代码。

CookieContainer cookies = new CookieContainer();
            HttpClientHandler handler = new HttpClientHandler();
            handler.CookieContainer = cookies;

            HttpClient client = new HttpClient(handler);
            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("username", "someusername"),
                new KeyValuePair<string, string>("password", "somepassword")
            });
            var response = await client.PostAsync("mysite.com/login", content);
            string res = await client.GetStringAsync("mysite.com/posts/getposts");

            Uri uri = new Uri("mysite.com/");

            var responseCookies = cookies.GetCookies(uri);
            foreach (Cookie cookie in responseCookies)
            {
                string name = cookie.Name;
                string value = cookie.Value;
            }

【问题讨论】:

  • CookieContainer 包含多少 cookie,使用 .Count
  • 1 - WPF 和控制台应用程序,0 - windows phone 8 应用程序:D

标签: c# cookies windows-phone-8 forms-authentication


【解决方案1】:

检查您尝试读取的 cookie 是否不是 HttpOnly cookie,在这种情况下不会暴露在 CookieContainer 中。如果是这种情况,这个answer 应该适合你。

【讨论】:

    【解决方案2】:

    也许this 会帮助你。检查您的repsoneCookies 属性,它是否包含cookie。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多