【发布时间】:2016-07-02 04:57:01
【问题描述】:
应用应该收到来自 LoginUser() 的 httpresponsemessage,但它没有响应。
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;
}
}
}
请帮忙!
【问题讨论】:
-
删除所有异步内容并确保其正常工作。
-
它是 UI 应用吗?如果是这样,那么您将导致
LoginUser( ... ).Result.IsSuccessStatusCode出现死锁。在这里阅读更多 - blog.stephencleary.com/2012/07/dont-block-on-async-code.html
标签: c# asynchronous async-await webapi2 httpresponsemessage