title: Async CTP for a PostSubmitter
body: <p>I'm trying to build a REST Client usign Async CTP. I'm new to the CTP and hence, afetr going through a number of examples on the internet, I got a clas built for just posting (GET or POST). Here is the class so far:</p>
  
  <pre><code>using System;
  using System.Collections.Specialized;
  using System.IO;
  using System.Net;
  using System.Text;
  using System.Threading.Tasks;
  using System.Web;
  
  namespace RESTClient.Core {
  
      /// &lt;summary&gt;
      /// Submits post data to a url.
      /// &lt;/summary&gt;
      public class PostSubmitter {
  
          #region Backing Store
          private string m_url = string.Empty;
          private NameValueCollection m_values = new NameValueCollection();
          private PostTypeEnum m_type = PostTypeEnum.Get;
          #endregion
  
          #region Constructors
          /// &lt;summary&gt;
          /// Default constructor.
          /// &lt;/summary&gt;
          public PostSubmitter() {
  
          }
  
          /// &lt;summary&gt;
          /// Constructor that accepts a url as a parameter
          /// &lt;/summary&gt;
          /// &lt;param name="url"&gt;The url where the post will be submitted to.&lt;/param&gt;
          public PostSubmitter(string url)
              : this() {
              m_url = url;
          }
  
          /// &lt;summary&gt;
          /// Constructor allowing the setting of the url and items to post.
          /// &lt;/summary&gt;
          /// &lt;param name="url"&gt;the url for the post.&lt;/param&gt;
          /// &lt;param name="values"&gt;The values for the post.&lt;/param&gt;
          public PostSubmitter(string url, NameValueCollection values)
              : this(url) {
              m_values = values;
          }
          #endregion
  
          #region Properties
          /// &lt;summary&gt;
          /// Gets or sets the url to submit the post to.
          /// &lt;/summary&gt;
          public string Url {
              get {
                  return m_url;
              }
              set {
                  m_url = value;
              }
          }
  
          /// &lt;summary&gt;
          /// Gets or sets the name value collection of items to post.
          /// &lt;/summary&gt;
          public NameValueCollection PostItems {
              get {
                  return m_values;
              }
              set {
                  m_values = value;
              }
          }
  
          /// &lt;summary&gt;
          /// Gets or sets the type of action to perform against the url.
          /// &lt;/summary&gt;
          public PostTypeEnum Type {
              get {
                  return m_type;
              }
              set {
                  m_type = value;
              }
          }
          #endregion
  
          /// &lt;summary&gt;
          /// Posts the supplied data to specified url.
          /// &lt;/summary&gt;
          /// &lt;returns&gt;a string containing the result of the post.&lt;/returns&gt;
          public async Task&lt;String&gt; Post() {
              StringBuilder parameters = new StringBuilder();
              for (int i = 0; i &lt; m_values.Count; i++) {
                  EncodeAndAddItem(ref parameters, m_values.GetKey(i), m_values[i]);
              }
              string result = await PostData(m_url, parameters.ToString());
              return result;
          }
  
          /// &lt;summary&gt;
          /// Posts the supplied data to specified url.
          /// &lt;/summary&gt;
          /// &lt;param name="url"&gt;The url to post to.&lt;/param&gt;
          /// &lt;returns&gt;a string containing the result of the post.&lt;/returns&gt;
          public async Task&lt;String&gt; Post(string url) {
              m_url = url;
              return await this.Post();
          }
  
          /// &lt;summary&gt;
          /// Posts the supplied data to specified url.
          /// &lt;/summary&gt;
          /// &lt;param name="url"&gt;The url to post to.&lt;/param&gt;
          /// &lt;param name="values"&gt;The values to post.&lt;/param&gt;
          /// &lt;returns&gt;a string containing the result of the post.&lt;/returns&gt;
          public async Task&lt;String&gt; Post(string url, NameValueCollection values) {
              m_values = values;
              return await this.Post(url);
          }
  
          /// &lt;summary&gt;
          /// Posts data to a specified url. Note that this assumes that you have already url encoded the post data.
          /// &lt;/summary&gt;
          /// &lt;param name="postData"&gt;The data to post.&lt;/param&gt;
          /// &lt;param name="url"&gt;the url to post to.&lt;/param&gt;
          /// &lt;returns&gt;Returns the result of the post.&lt;/returns&gt;
          private async Task&lt;String&gt; PostData(string url, string postData) {
              HttpWebRequest request = null;
              if (m_type == PostTypeEnum.Post) {
                  Uri uri = new Uri(url);
                  request = (HttpWebRequest)WebRequest.Create(uri);
                  request.Method = "POST";
                  request.ContentType = "application/x-www-form-urlencoded";
                  request.ContentLength = postData.Length;
                  using (Stream writeStream = await request.GetRequestStreamAsync()) {
                      UTF8Encoding encoding = new UTF8Encoding();
                      byte[] bytes = encoding.GetBytes(postData);
                      writeStream.Write(bytes, 0, bytes.Length);
                  }
              }
              else {
                  Uri uri = new Uri(url + "?" + postData);
                  request = (HttpWebRequest)WebRequest.Create(uri);
                  request.Method = "GET";
              }
  
              string result = string.Empty;
  
              using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync()) {
                  using (Stream responseStream = response.GetResponseStream()) {
                      using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8)) {
                          result = readStream.ReadToEnd();
                      }
                  }
              }
  
              return result;
          }
  
          /// &lt;summary&gt;
          /// Encodes an item and ads it to the string.
          /// &lt;/summary&gt;
          /// &lt;param name="baseRequest"&gt;The previously encoded data.&lt;/param&gt;
          /// &lt;param name="dataItem"&gt;The data to encode.&lt;/param&gt;
          /// &lt;returns&gt;A string containing the old data and the previously encoded data.&lt;/returns&gt;
          private void EncodeAndAddItem(ref StringBuilder baseRequest, string key, string dataItem) {
              if (baseRequest == null) {
                  baseRequest = new StringBuilder();
              }
              if (baseRequest.Length != 0) {
                  baseRequest.Append("&amp;");
              }
              baseRequest.Append(key);
              baseRequest.Append("=");
              baseRequest.Append(HttpUtility.UrlEncode(dataItem));
          }
  
      }
  
  }
  </code></pre>
  
  <p>And this is how I'm using it:</p>
  
  <pre><code>    private void ButtonSubmit_Click(object sender, EventArgs e) {
          ButtonReset.Enabled = false;
          TextResponse.Text = String.Empty;
          TextResponse.Text += "Begining..." + Environment.NewLine;
  
          try {
              TextResponse.Text += Task.Factory.StartNew(() =&gt; PostSomeData().Wait());
              //TextResponse.Text +=  PostSomeData();
              TextResponse.Text += Environment.NewLine;
              TextResponse.Text += "Function Done!" + Environment.NewLine;
          }
          catch (Exception ex) {
              TextResponse.Text += "Exception!" + Environment.NewLine + "Message: " + ex.Message + Environment.NewLine;
          }
          finally {
              ButtonReset.Enabled = true;
              TextResponse.Text += "Function Ended!";
          }
      }
  
      private async Task&lt;String&gt; PostSomeData() {
          PostSubmitter post = new PostSubmitter();
          post.Url = TextURL.Text.Trim();
  
          post.PostItems.Add(TextParam01.Text.Trim(), TextValue01.Text.Trim());
          post.PostItems.Add(TextParam02.Text.Trim(), TextValue02.Text.Trim());
          post.PostItems.Add(TextParam03.Text.Trim(), TextValue03.Text.Trim());
          post.PostItems.Add(TextParam04.Text.Trim(), TextValue04.Text.Trim());
          post.PostItems.Add(TextParam05.Text.Trim(), TextValue05.Text.Trim());
          post.PostItems.Add(TextParam06.Text.Trim(), TextValue06.Text.Trim());
          post.PostItems.Add(TextParam07.Text.Trim(), TextValue07.Text.Trim());
          post.PostItems.Add(TextParam08.Text.Trim(), TextValue08.Text.Trim());
          post.PostItems.Add(TextParam09.Text.Trim(), TextValue09.Text.Trim());
          post.PostItems.Add(TextParam10.Text.Trim(), TextValue10.Text.Trim());
          post.PostItems.Add(TextParam11.Text.Trim(), TextValue11.Text.Trim());
          post.PostItems.Add(TextParam12.Text.Trim(), TextValue12.Text.Trim());
          post.Type = PostTypeEnum.Post;
  
          return await post.Post();
      }
  </code></pre>
  
  <p>The behaviour is not quite as expected. The line <code>TextResponse.Text += Task.Factory.StartNew(() =&gt; PostSomeData().Wait());</code> whiz-by and I get no exceptions and here is the resulting string:</p>
  
  <p>Begining...
  System.Threading.Tasks.Task
  Function Done!
  Function Ended!</p>
  
  <p>Now, if I'm using a POST, I get an exception after the above. Digging into the exception reveals a <em>500 Internal Server Error</em></p>
  
  <p>However, if i'm using GET, nothing happens. No exception and just the same final result.</p>
  
  <p>Am I doing something wrong in the PostSubmitter Class?</p>
  
  <p>Here is the UI shot with the paramaeters:
  <a href="https://i.stack.imgur.com/tqaca.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tqaca.png" alt="UI Screen Shot"></a>
  </p>
  
  <p>Regards.</p>
  
  <p><strong>Update #1</strong>
  I have modified the click event on the UI as well. However</p>
  
  <ul>
  <li>It only works if the PostType is <em>GET</em>. POST is not working.</li>
  <li>The UI <em>hangs</em> while the operation lasts</li>
  </ul>
  
  <p>The modifications:</p>
  
  <pre><code>    private async void ButtonSubmit_Click(object sender, EventArgs e) {
          ButtonReset.Enabled = false;
          TextResponse.Text = String.Empty;
          TextResponse.Text += "Begining..." + Environment.NewLine;
  
          try {
              TextResponse.Text += await PostSomeData();
              TextResponse.Text += Environment.NewLine;
              TextResponse.Text += "Function Done!" + Environment.NewLine;
          }
          catch (Exception ex) {
              TextResponse.Text += "Exception!" + Environment.NewLine + "Message: " + ex.Message + Environment.NewLine;
          }
          finally {
              ButtonReset.Enabled = true;
              TextResponse.Text += "Function Ended!";
          }
      }
  </code></pre>