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
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;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With