在Xamarin.forms中从httprequest中检索cookie

时间:2017-04-27 09:04:50

标签: web-services cookies xamarin.forms session-cookies

帮助我,我无法从httprequest获取cookie,我尝试了plugins.settings,我也尝试了pcl共享,我在这个问题上待了一个月

        public async Task<bool> PostAsync(AuthUser user)
    {
        var CookieContainer = new CookieContainer();
        var handler = new HttpClientHandler() { CookieContainer = 
        CookieContainer };
        var _client = new HttpClient(handler);

        IEnumerable<string> cookieStrings = null;
        //var httpClient = new HttpClient();
        _client.DefaultRequestHeaders.Accept.Add(new 
         MediaTypeWithQualityHeaderValue("application/json"));
        var json = JsonConvert.SerializeObject(user);
            HttpContent httpContent = new StringContent(json);

            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            try
            {
                string url = WebServiceUrl + "j_spring_security_check?j_username=" + user.j_username +"&j_password=" + user.j_password + "&ajax=true";
            HttpResponseMessage result = await _client.PostAsync(url, httpContent);

                IEnumerable<string> cookies;
                if (result.Headers.TryGetValues("set-cookie", out cookies))
                {
                    foreach (var c in cookies)
                    {
                    await App.Current.MainPage.DisplayAlert("Cookie", c , "OK");
                    }
                }
                if (result.IsSuccessStatusCode)
                {
                    using (var responsecontent = result.Content)
                    {
                    string resultString = responsecontent.ReadAsStringAsync().Result;

                        var response = JsonConvert.DeserializeObject<AuthUser>(resultString);
                        if (response.error != null)
                        {
                            await App.Current.MainPage.DisplayAlert("Error", response.result.error, "OK");
                            return false;
                        }
                        else if (response.result.success.Equals("1"))
                        {
                        App.Current.MainPage = new NavigationPage(new TimelineMenuPage(response.result.user_id.ToString(), response.result.token));

                        return true;
                        }

                    }
                }
                return result.IsSuccessStatusCode;
        }

            catch (Exception e)
            {
               await App.Current.MainPage.DisplayAlert("Alert", e.ToString(), "OK");
              throw;
            }
        }

调试时跳过这部分:

if (result.Headers.TryGetValues("set-cookie", out cookies))
{
foreach (var c in cookies)
{
await App.Current.MainPage.DisplayAlert("Cookie", c , "OK");
}
}

**然后我进入CookieContainer count = 0 **

1 个答案:

答案 0 :(得分:0)

由于您已经在使用CookieContainer,并且您将了解Uri是从中获取的,为什么不直接从Container获取Cookie,而不是set-cookie命令,那么您将拥有解析。

cookieContainer.GetCookies(new Uri("mydomain.com"));

在您的HttpRequest之后,它会自动将它们放入CookieContainer。