2014-01-09 55 views
0

我必須做表單身份驗證才能使用服務,但我無法獲取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; 
      } 
+0

'CookieContainer'包含多少Cookie,使用'.Count'? – FlashTek

+0

1 - WPF和控制檯應用程序,0 - Windows Phone 8應用程序:D – user3172704

回答

0

也許this會幫助你。檢查你的repsoneCookies屬性,它是否包含cookies。

1

檢查您嘗試讀取的Cookie不是HttpOnly Cookie,在這種情況下CookieContainer中不會公開該Cookie。如果是這種情況,這answer應該爲你工作。