我必須做表單身份驗證才能使用服務,但我無法獲取Cookie。一個解決方案是每次有兩個請求登錄頁面,一個請求我想獲得的資源。我在wpf和控制檯應用程序上做了同樣的事情,它運行良好,但不是在Windows Phone 8中。這裏是我的代碼。無法獲取Cookie與HttpClient和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'? – FlashTek
1 - WPF和控制檯應用程序,0 - Windows Phone 8應用程序:D – user3172704