title: Await HttpClient and do-while loop
body: <p>I have a method to POST some data to server. Server may return aswer means I need change URL and send data to other server. I use for it <code>HttpClient</code> class from 4.5 framework. I use either do-while loop to repeat requests until I do not need redirecton. But there is a problem.</p>
  
  <p>The question is why if I create <code>HttpClient</code> instance outside loop, second <code>await</code> didn't happen and my programm get out of loop, but if I create <code>HttpClient</code> instance inside the loop again and again - all is fine? Can I reuse one <code>HttpClient</code> for several POST requsts inside do-while loop inside async method?</p>
  
  <p>My working code sample:
  </p>
  
  <pre><code>public async Task&lt;bool&gt; GameLogin()
  {
      JToken r;
      do
      {
          var clientHandler = new HttpClientHandler
          {
              CookieContainer = this.myCContainer,
              AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
          };
          var client = new HttpClient(clientHandler);
          client.DefaultRequestHeaders.Add("client-ver", Versoin);
          client.DefaultRequestHeaders.Add("method", "SignIn");
          client.DefaultRequestHeaders.Add("authKey", AppParams["auth_key"].ToString());
  
          var content = new StringContent(this.SendStr);
  
          var answer = await client.PostAsync(this.CurrentUrl, content);
          var rawString = await answer.Content.ReadAsStringAsync();
          DinamicData = JObject.Parse(rawString);
          r = DinamicData["r"];
          if (r == null) continue;
          this.CurrentUrl = string.Format("http://{0}/main.ashx", r);
      } while (r != null);
  
      return DinamicData.Type != JTokenType.Null;
  }
  </code></pre>
  
  <p>My did not working code sample:
  </p>
  
  <pre><code>public async Task&lt;bool&gt; GameLogin()
  {
      var clientHandler = new HttpClientHandler
      {
          CookieContainer = this.myCContainer,
          AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
      };
      var client = new HttpClient(clientHandler);
      client.DefaultRequestHeaders.Add("client-ver", Versoin);
      client.DefaultRequestHeaders.Add("method", "SignIn");
      client.DefaultRequestHeaders.Add("authKey", AppParams["auth_key"].ToString());
  
      var content = new StringContent(this.SendStr);
  
      JToken r;
      do
      {
          var answer = await client.PostAsync(this.CurrentUrl, content);
          var rawString = await answer.Content.ReadAsStringAsync();
          DinamicData = JObject.Parse(rawString);
          r = DinamicData["r"];
          if (r == null) continue;
          this.CurrentUrl = string.Format("http://{0}/main.ashx", r);
      } while (r != null);
  
      return DinamicData.Type != JTokenType.Null;
  }
  </code></pre>
  
  <p>And my second code did not wait <code>await</code> in secont loop turn.</p>