title: c# Post http request slow
body: <p>I am busy with a c# application that must upload data to a site. I use http post to upload the data but it is too slow for our application. It takes around 1.5 seconds to upload a small amount of data. I tried two different approaches.</p>
  
  <p>Approach 1, using WebRequest:</p>
  
  <pre><code>WebRequest request = WebRequest.Create(packet.url.ToString());
                  request.Method = "POST";
                  request.Proxy = null;
                  byte[] byteArray = Encoding.UTF8.GetBytes(packet.postData);
                  request.ContentType = "application/x-www-form-urlencoded";
                  request.ContentLength = byteArray.Length;
                  Stream dataStream = request.GetRequestStream();
                  dataStream.Write(byteArray, 0, byteArray.Length);
                  dataStream.Close();
                  WebResponse response = request.GetResponse();
                  dataStream = response.GetResponseStream();
                  StreamReader reader = new StreamReader(dataStream);
                  string responseFromServer = reader.ReadToEnd();
                  reader.Close();
                  dataStream.Close();
                  response.Close();
  </code></pre>
  
  <p>Approach2, using WebClient:</p>
  
  <pre><code>string responseFromServer;
                  using (WebClient wc = new WebClient())
                  {
                      wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                      responseFromServer = wc.UploadString(packet.url, packet.postData);
                  }
  </code></pre>
  
  <p>Both these have exactly the same delay. I tested the same thing in javascript. I used a xmlhttprequest to post the same data to the server and it took less than 0.5s to upload the data. Please let me know if there is a faster way of doing this in c#.</p>