我在调用 HttpClient 类和异步调用时遇到问题。我从 page_load 调用函数 List()。调用从 HttpResponseMessage response = await client.GetAsync(str); 行返回 并且永远不会回来完成它。
我不明白我在做什么错误。以下是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
Task<string> s= List(product);
}
protected async Task<string> List(string ProductType)
{
string str = "http://xx.xx.com/wiki/api.php";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", "username", "password"))));
HttpResponseMessage response = await client.GetAsync(str);
response.EnsureSuccessStatusCode();
string content = await response.Content.ReadAsStringAsync();
}
return content;
}
它从不执行以下行。
response.EnsureSuccessStatusCode();
string content = await response.Content.ReadAsStringAsync();
请帮忙。提前致谢。