简体   繁体   中英

How use cookie Windows Phone c#

I have problem. I have code on the main page:

      private async Task<string> Login(string username, string password)
    {
        try
        {
            string url = "myurl.com/page1";
            Uri address = new Uri(url);
            var postData = new List<KeyValuePair<string, string>>
                           {
                 new KeyValuePair<string, string>("Login", username),
                 new KeyValuePair<string, string>("Password", password)
                           };
            CookieContainer cookieJar = new CookieContainer();
            HttpContent content = new FormUrlEncodedContent(postData);
            var handler = new HttpClientHandler
            {
                CookieContainer = cookieJar,
                UseCookies = true,
                UseDefaultCredentials = false
            };

            var client = new HttpClient(handler)
            {
                BaseAddress = address
            };

            HttpResponseMessage response = await client.PostAsync(url, content);

            string UrlBase = "myurl.com/page1";
            Uri uri = new Uri(UrlBase);
            var responseCookies = cookieJar.GetCookies(uri);
            foreach (Cookie cookie in responseCookies)
            {
                string cookieName = cookie.Name;
                string cookieValue = cookie.Value; 
            }    
            response.EnsureSuccessStatusCode();
            string body = await response.Content.ReadAsStringAsync();
            return body;
        }
         catch (Exception e)
        {

            return e.ToString();

        }

    }

But in debug I cant see cookies. And his number is 0. And I don`t know, how can I get myurl.com/page2 on the Page2.xaml? How to use and save 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