我正在尝试通过调用api在Xamarin应用中显示登录消息...但它显示
未处理的异常:
System.Net.WebException: 发生
在此行
var response =await httpClient.GetStringAsync("http://localhost:40987/Api/Home/Post?username="+username.Text+"&password="+password.Text);
我尝试了所有建议,但没有摆脱困境
private async void Button_ClickedAsync(object sender, EventArgs e)
{
User user = new User
{
UserName = username.Text,
Password = password.Text
};
var Json = Newtonsoft.Json.JsonConvert.SerializeObject(user);
HttpContent httpContent = new StringContent(Json);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/Json");
var httpClient = new HttpClient();
//httpClient.GetAsync("windows.digitalgramsoft.com/Api/Home");
//DisplayAlert("Added", "Your Data has been added", "OK")
var response =await httpClient.GetStringAsync("http://localhost:40987/Api/Home/Post?username="+username.Text+"&password="+password.Text);
var login = JsonConvert.DeserializeObject<List<User>>(response);
}
只想使用api显示登录消息或错误消息
答案 0 :(得分:0)
尝试一下:
try{
var Json = Newtonsoft.Json.JsonConvert.SerializeObject(user);
HttpContent httpContent = new StringContent(Json);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/Json");
var httpClient = new HttpClient();
//httpClient.GetAsync("windows.digitalgramsoft.com/Api/Home");
//DisplayAlert("Added", "Your Data has been added", "OK")
var response =await httpClient.GetStringAsync("http://localhost:40987/Api/Home/Post?username="+username.Text+"&password="+password.Text);
var login = JsonConvert.DeserializeObject<List<User>>(response);
}catch(Exception ex){
Console.WriteLine(ex.Message);
}
然后使用断点检查Exception
对象中的内容。从那里,您可以跟踪源。