我在調用HttpClient類和異步調用時遇到問題。我從page_load調用函數List()。從行HttpResponseMessage返回的響應response = await client.GetAsync(str);永遠不會回來完成它。asp.net中的HttpClient調用
我不明白我在做什麼錯誤。以下是我的代碼:
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();
請幫忙。提前致謝。
感謝斯蒂芬對我的幫助。它有助於。 – Hiral 2013-03-13 15:24:41