【发布时间】: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