应用程序应该接收来自LoginUser()的LoginUser,但它没有响应。
private void button1_Click(object sender, EventArgs e)
{
if (LoginUser(tUser.Text, Password.Text).Result.IsSuccessStatusCode)
{
Notifier.Notify("Successfully logged in.. Please wait!");
}
else
{
Notifier.Notify("Please check your Credential..");
}
} public async Task<HttpResponseMessage> LoginUser(string userid, string password)
{
string URI = "http://api.danubeco.com/api/userapps/authenticate";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("c291cmF2OmtheWFs");
using (var response = await client.GetAsync(String.Format("{0}/{1}/{2}", URI, userid, password)))
{
return response;
}
}
}请帮帮我!
发布于 2016-03-16 11:11:16
您正在阻塞UI线程并导致死锁。从斯蒂芬·克利里的博客 (只需用LoginUser方法替换GetJsonAsync,用client.GetAsync替换GetStringAsync ):
这就是所发生的事情,首先是顶层方法(Button1_Click表示UI / MyController.Get表示ASP.NET):
以及简单的可用解决方案(也来自博客):
第二个解决方案建议将button1_Click更改为:
private async void button1_Click(object sender, EventArgs e)
{
if ((await LoginUser(tUser.Text, Password.Text)).IsSuccessStatusCode)
{
Notifier.Notify("Successfully logged in.. Please wait!");
}
else
{
Notifier.Notify("Please check your Credential..");
}
}https://stackoverflow.com/questions/36033532
复制相似问题