Search code examples
c#windows-8dotnet-httpclientwinrt-async

Calling HttpClient from UI in WinRT - Nothing happens


I'm trying to get the content of a website in a Windows 8 Metro App. I'm still a bit confused about the asyn and wait methods.
But this is what I have:

private void HelloButton_Click(object sender, RoutedEventArgs e)
{  
    GetPage().Wait();
}

private async Task GetPage()
{
      HttpClient client = new HttpClient();

      HttpResponseMessage response = await client.GetAsync("http://www.supertext.ch");
      response.EnsureSuccessStatusCode();
      string responseBody = await response.Content.ReadAsStringAsync();
}

Unfortuantely, nothing happens. If try to debug it, it just stops at GetAsync().
Do I need to make the HelloButton_Click function also async?
Even though I wait for the completion of the function GetPage()?
Can I call network requests directly from the GUI thread?


Solution

  • Don't call Wait on the task. await it. This is a common beginners bug.