I am facing problem in calling HttpClient class and async call. I call the function List() from page_load. The calls return from the line HttpResponseMessage response = await client.GetAsync(str); and never comes back to finish it.
I don't understand what mistake I am doing. Following is my code:
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;
}
It never executes following lines.
response.EnsureSuccessStatusCode();
string content = await response.Content.ReadAsStringAsync();
Please help. Thanks in advance.