title: Deal with async/Task for call a RESTful web service in Xamarin Forms
body: <p>I've to call a RESTful web service in my Xamarin Forms app.
  I'd like to add an Activity Indicator in order to avoid user actions while web service call is running; so I think that I've to use C# async/Task, correct?</p>
  
  <p>The web service that I've to consume is an existing one then I've to follow previous specification. In this case I've to execute the login that is composed by two steps. In the first one I've to get the real base url of by POSTing to a fixed url and after POST user credentials to an url composed by the returned url + a fixed part.</p>
  
  <p>I made some try and what I see is that the string where I want to store the web service response content is empty becouse the code continues to run even if web service call is not finished and I don't know how to deal it.</p>
  
  <p>Here is my code: </p>
  
  <pre><code>HttpClient client = new HttpClient();
  
  public async Task&lt;string&gt; Post(string url, FormUrlEncodedContent parameters)
  {
      using (var response = await client.PostAsync(url, parameters))
      {
          return await response.Content.ReadAsStringAsync();
      };        
  } 
  
  // Used to bind ActivityIndicator IsRunning property in XAML file
  public string ShowActivityIndicator { get; set; }
  
  public async void ButtonClickMethod()
  {
      ShowActivityIndicator = true;
  
      await GetLink();
      var user = await GetLink();
  
      ShowActivityIndicator = false;
  
      App.Navigation.PushAsync(new HomePage(user));
  }
  
  public string BaseUrl { get; set; }
  public string LoginUrl { get { return BaseUrl + Constants.LoginUrlPart; }
  
  public async Task GetLink(FormUrlEncodedContent parameters)
  {
      var json = await Post(Constants.LinkUrl, parameters);
  
      // json is empty becouse web service call hasn't yet finished
      var response = JsonConvert.DeserializeObject&lt;Link&gt;(json);
  
      BaseUrl = response.Link;
  }
  
  public async Task&lt;User&gt; Login(FormUrlEncodedContent parameters)
  {
      var json = await Post(LoginUrl);
  
      // json is empty becouse web service call hasn't yet finished
      var response = JsonConvert.DeserializeObject&lt;User&gt;(json);
  
      return response;
  }
  </code></pre>
  
  <p>Thank you for your help.</p>