title: How to make ordinary WebRequest async and awaitable?
body: <p>I need to make the following code async and awaitable.</p>
  
  <p>I need to get a lot of data from the web server, and then this data will be used to populate the xaml page in my application.</p>
  
  <p>So, I need the DefLogin() method to be awaitable.</p>
  
  <p>Is it possible?</p>
  
  <pre><code>public void DefLogin()
      {
          postData = "My Data To Post";
          var url = new Uri("Url To Post to", UriKind.Absolute);
          webRequest = WebRequest.Create(url);
          webRequest.Method = "POST";
          webRequest.ContentType = "text/xml";
          webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);
      }
  
      public void GetRequestStreamCallback(IAsyncResult asynchronousResult)
      {
          webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
          Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
          byte[] byteArray = Encoding.UTF8.GetBytes(postData);
          postStream.Write(byteArray, 0, byteArray.Length);
          postStream.Close();
          Debug.WriteLine("Start BEGINGetResponse");
          webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
      }
  
      public void GetResponseCallback(IAsyncResult asynchronousResult)
      {
          try
          {
              HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
              HttpWebResponse response;
              response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
              Stream streamResponse = response.GetResponseStream();
              StreamReader streamReader = new StreamReader(streamResponse);
              string Response = streamReader.ReadToEnd();
              streamResponse.Close();
              streamReader.Close();
              response.Close();
              if (Response == "")
              {
                  //show some error msg to the user        
                  Debug.WriteLine("ERROR");
  
              }
              else
              {
                  //Your response will be available in "Response" 
                  Debug.WriteLine(Response);
              }
          }
          catch (WebException)
          {
              //error    
          }
      }
  </code></pre>
  
  <p>I saw this question on StackOverflow: <a href="https://stackoverflow.com/questions/14577346/converting-ordinary-http-post-web-request-with-async-and-await">Converting ordinary Http Post web request with Async and Await</a>, but I could not understand the answer properly.</p>
  
  <p>Please can anyone help? I would be really grateful!</p>