简体   繁体   中英

Can't get cookie with HttpClient and windows phone 8

I have to do forms authentication to use a service, but I can't get cookie. One solution is every time did two requests one to login page and one to resource I want to get. I did the same thing on wpf and console apps and it works well but not in windows phone 8. here is my code.

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;
            }

Check that the cookie you are trying to read is not an HttpOnly cookie, in which case will not be exposed in the CookieContainer. If that's the situation, this answer should work for you.

Maybe this will help you. Check your repsone 's Cookies property, whether it contains the cookies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM