我有这个结构,我也使用 System.Net.Http 和 Newtonsoft 。 我需要收到webservice响应并在我的课程中转换它,但我不知道如何使用HttpResponseMessage和我红色的帖子没有帮助我。 这是一个Xamarin.forms项目。
public static async Task<User> PostLoginAsync(string login, string senha)
{
using (var client = new HttpClient())
{
try
{
login = "email@hotmail.com";
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;
}
}
}
我的班级:
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; }
}
如果你能帮助我...我会很感激。 不管怎样,谢谢你
答案 0 :(得分:3)
您需要等待来自HttpResponseMessage的内容。
public static async Task<User> PostLoginAsync(string login, string senha)
{
using (var client = new HttpClient())
{
try
{
login = "email@hotmail.com";
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;
}
}
}