title: Performance of HttpWebRequest using POST
body: <p>I have a small tool I use for testing a webservice.</p>
  
  <p>It can either call the webservice using POST or using GET.</p>
  
  <p>The code for using POST is</p>
  
  <pre><code>public void PerformRequest()
  {
    WebRequest webRequest = WebRequest.Create(_uri);
  
    webRequest.ContentType = "application/ocsp-request";
    webRequest.Method = "POST";
    webRequest.Credentials = _credentials;
    webRequest.ContentLength = _request.Length;
    ((HttpWebRequest)webRequest).KeepAlive = false;
  
    using (Stream st = webRequest.GetRequestStream())
      st.Write(_request, 0, _request.Length);
  
    using (HttpWebResponse httpWebResponse = (HttpWebResponse)webRequest.GetResponse())
    using (Stream responseStream = httpWebResponse.GetResponseStream())
    using (BufferedStream bufferedStream = new BufferedStream(responseStream))
    using (BinaryReader reader = new BinaryReader(bufferedStream))
    {
      if (httpWebResponse.StatusCode != HttpStatusCode.OK)
        throw new WebException("Got response status code: " + httpWebResponse.StatusCode);
  
      byte[] response = reader.ReadBytes((int)httpWebResponse.ContentLength);
      httpWebResponse.Close();
    }      
  }
  </code></pre>
  
  <p>The code for using GET is:</p>
  
  <pre><code>protected override void PerformRequest()
  {
    WebRequest webRequest = WebRequest.Create(_uri + "/" + Convert.ToBase64String(_request));
  
    webRequest.Method = "GET";
    webRequest.Credentials = _credentials;
    ((HttpWebRequest)webRequest).KeepAlive = false;
  
    using (HttpWebResponse httpWebResponse = (HttpWebResponse)webRequest.GetResponse())
    using (Stream responseStream = httpWebResponse.GetResponseStream())
    using (BufferedStream bufferedStream = new BufferedStream(responseStream))
    using (BinaryReader reader = new BinaryReader(bufferedStream))
    {
      if (httpWebResponse.StatusCode != HttpStatusCode.OK)
        throw new WebException("Got response status code: " + httpWebResponse.StatusCode);
  
      byte[] response = reader.ReadBytes((int)httpWebResponse.ContentLength);
      httpWebResponse.Close();
    }
  }
  </code></pre>
  
  <p>As you can see the code is quite similar. If anything, I would expect the GET-method to be slightly slower, as it has to encode and transmit the data in Base64.</p>
  
  <p>But when I run it, I see that the POST-method uses far more processing power than the GET-method. On my machine, I can run 80 threads of the GET-method using approximately 5% CPU, while 80 threads of the POST-method uses 95% CPU.</p>
  
  <p>Is there something inherently more expensive about using POST? Is there anything I can do to optimize the POST-method? I cannot reuse the connection, as I want to simulate requests from different clients.</p>
  
  <p>dotTrace reports that 65% of the processing-time is spent in webRequest.GetResponse() when using POST.</p>
  
  <p>The underlying webservice uses Digest-Authentication if that makes any difference.</p>