title: how to wait until a web request with HttpWebRequest is finished?
body: <p>I am doing a web request to login in a my web services and I have a problem. I need to wait until the login is finished before end the method that launchs the login because I need to return a boolean value that represents if the login was correct or not. In the following code I put a comment where I need wait for the result of "httpWebRequest.BeginGetResponse(...)"</p>
  
  <pre><code>public async Task&lt;bool&gt; login(string user, string passwrd)
  {
      mLoginData.setUserName(user);
      mLoginData.setPasswd(passwrd);
  
      string serviceURL = mBaseURL + "/pwpcloud/service/user/login";
  
      //build the REST request
      // HTTP web request
      var httpWebRequest = (HttpWebRequest)WebRequest.Create(serviceURL);
      httpWebRequest.ContentType = "application/json";
      httpWebRequest.Method = "POST";
  
      // Write the request Asynchronously 
      using (var stream = await Task.Factory.FromAsync&lt;Stream&gt;(httpWebRequest.BeginGetRequestStream, httpWebRequest.EndGetRequestStream, null))
      {
  
          string parameters = "{\"username\":\"" + user + "\",\"password\":\"" + passwrd + "\"}";
          byte[] byteArray = Encoding.UTF8.GetBytes(parameters);
  
          // Write the bytes to the stream
          await stream.WriteAsync(byteArray, 0, byteArray.Length);
      }
      //httpWebRequest.BeginGetResponse(new AsyncCallback(OnGettingLoginResponse), httpWebRequest);
      httpWebRequest.BeginGetResponse(r =&gt;
         {
             using (HttpWebResponse response = (HttpWebResponse)httpWebRequest.EndGetResponse(r))
             {
                 string data;
  
                 // Read the response into a Stream object. 
                 Stream responseStream = response.GetResponseStream();
                 using (var reader = new StreamReader(responseStream))
                 {
                     data = reader.ReadToEnd();
                 }
                 responseStream.Close();
                 if (response.StatusCode == HttpStatusCode.OK)
                 {
                     dynamic jsonData = JObject.Parse(data);
                     string token = jsonData.token;
                     mLoginData.setToken(token);
                     string userId = jsonData.userId;
                     mLoginData.setUserID(userId);
                     mLoginData.setLogged(true);
                     //guardamos los valores en el isolatedStorageSettings para que persistan. 
                     App.settings.Clear();
                     App.settings.Add("userId", mLoginData.getUserID());
                     App.settings.Add("token", mLoginData.getToken());
                     App.settings.Add("userName", mLoginData.getUserName());
                     App.settings.Add("passwd", mLoginData.getPasswd());
                     App.settings.Add("logged", mLoginData.isLogged());
                     App.settings.Save();
                 }
                 else
                 {
                     if (CultureInfo.CurrentUICulture.Name.ToString().Contains("ES"))
                     {
                         if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.BadRequest)
                         {
                             MessageBox.Show("Usuario o contraseña incorrectos");
                         }
                         else
                         {
                             MessageBox.Show("Error de conexión.");
                         }
                     }
                     else
                     {
                         if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.BadRequest)
                         {
                             MessageBox.Show("User or password were incorrect");
                         }
                         else
                         {
                             MessageBox.Show("NNetwork connection error");
                         }
                     }
                 }
             }
         }, null);
      //here i need wait for the result of the webrequest to return true if the login was successfull.
      return mLoginData.isLogged();
  }
  </code></pre>
  
  <p>I read a lot about this buf I didn't find the solution.
  Thanks everyone!!!</p>