title: httpWebRequest (The underlying connection was closed: The connection was closed unexpectedly.)
body: <p>I am developing an C# application which logs data from a webserver. It sends the following post request to the webserver and awaits for the response.</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Function for obtaining testCgi data 
      /// &lt;/summary&gt;
      /// &lt;param name="Parameters"&gt;&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      private string HttpmyPost(string Parameters)
      {
          string str = "No response";
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriTestCGI);
          request.Method = "POST";
  
          byte[] bytes = Encoding.UTF8.GetBytes(Parameters);
          request.ContentLength = bytes.Length;
  
          Stream requestStream = request.GetRequestStream();
          requestStream.Write(bytes, 0, bytes.Length);
          requestStream.Close();
  
              WebResponse response = request.GetResponse();
              Stream stream = response.GetResponseStream();
              StreamReader reader = new StreamReader(stream);
  
              try
              {
                  var result = reader.ReadToEnd();
              stream.Dispose();
              str = result.ToString();
              reader.Dispose();
          }
          catch (WebException ex)
          {
              //System.Windows.Forms.MessageBox.Show(ex.Message);
              System.Diagnostics.Trace.WriteLine(ex.Message);
  
          }
          finally
          {
              request.Abort();
          }
          return str;
      }
  </code></pre>
  
  <p>I am getting the error </p>
  
  <pre><code>&gt; "The underlying connection was closed: The connection was closed
  &gt; unexpectedly"
  </code></pre>
  
  <p>I have tried to debug the error, and I have used fiddler in order to check the post request as given from Firefox. To my surprise whenever Fiddler was my program was working perfectly. When I close fiddler I am having the same error. </p>
  
  <p>I suspect that since Fiddler is acting as a proxy it may change some of the settings. 
  I have tried using webclient and the result was the same. </p>
  
  <p>When I tried coding the request in python, everything worked as it should without no problem. Of cource I have the option of installing IronPython and wrapping that particular function, however I consider this overkill and lacking elegance, so I am pursuing a leaner approach. I suspect this is nothing more that a setting adjustment.</p>
  
  <p>I have tried modifying and in my case it is indifferent. </p>
  
  <pre><code>request.Accept 
  request.ReadWriteTimeout 
  request.Timeout 
  request.UserAgent 
  request.Headers
  request.AutomaticDecompression 
  request.Referer
  request.AllowAutoRedirect
  //request.TransferEncoding 
  request.Expect
  request.ServicePoint.Expect100Continue 
  request.PreAuthenticate 
  request.KeepAlive 
  request.ProtocolVersion 
  request.ContentType
  </code></pre>
  
  <p>With or without the above adjustments the code works when Fiddler is capturing data.</p>
  
  <p>Also it might be noteworthy that the program yields the error at </p>
  
  <pre><code>WebResponse response = request.GetResponse();
  </code></pre>
  
  <p>UPDATE:
  Following @EricLaw suggestions I looked into Latency.
  I found this article 
  <a href="https://stackoverflow.com/questions/11362136/httpwebrequest-gets-slower-when-adding-an-interval">HttpWebRequest gets slower when adding an Interval</a>
  which suggested turning of the Nagle Algorithm. 
  Now there are no closed connections, although there is a small lag in the overall response (when I use winforms, and not async).</p>