Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert a HttpResponseMessage to an object

I have this structure, I am using System.Net.Http and Newtonsoft as well. I need receive the webservice response and convert it in my class, but I don't know how to do that with a HttpResponseMessage and the posts that I've red hasn't helped me. It's a Xamarin.forms project.

 public static async Task<User> PostLoginAsync(string login, string senha)
    {
        using (var client = new HttpClient())
        {
            try
            {
                login = "[email protected]";
                senha = "1111111";

                var content = new FormUrlEncodedContent(new[]
                    {
                        new KeyValuePair<string, string>("id", "1200"),
                        new KeyValuePair<string, string>("email", login),
                        new KeyValuePair<string, string>("password", senha),
                        new KeyValuePair<string, string>("json", "1"),
                     });

                HttpResponseMessage response = await client.PostAsync("http://ws.site.com", content);

                return null;
            }

            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return null;
            }
        }
    }

my class:

class User
{
    public string codigo { get; set; }
    public string nome { get; set; }
    public string email { get; set; }
    public string senha { get; set; }
    public string imagem { get; set; }
    public DateTime dataDeNasc { get; set;}
    public string cidade { get; set; }
    public string estado { get; set; }
    public string telefone { get; set; }
    public string sexo { get; set; }
}

If you can help me...I would appreciate that. Thank you anyways

like image 910
Joyce de Lanna Avatar asked Aug 07 '17 15:08

Joyce de Lanna


1 Answers

You need to await the content from the HttpResponseMessage.

public static async Task<User> PostLoginAsync(string login, string senha)
{
    using (var client = new HttpClient())
    {
        try
        {
            login = "[email protected]";
            senha = "1111111";

            var content = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("id", "1200"),
                    new KeyValuePair<string, string>("email", login),
                    new KeyValuePair<string, string>("password", senha),
                    new KeyValuePair<string, string>("json", "1"),
                 });

            HttpResponseMessage response = await client.PostAsync("http://ws.site.com", content);

            var responseContent = await response.Content.ReadAsStringAsync();
            var user = JsonConvert.DeserializeObject<User>(responseContent);

            return user;
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
            return null;
        }
    }
}
like image 127
tequila slammer Avatar answered Oct 04 '22 22:10

tequila slammer