title: Why does HttpWebRequest time out even when transfer is ongoing?
body: <p>We have the same error as <a href="http://social.msdn.microsoft.com/Forums/en-US/06ff0cd7-e8ec-44ad-86bd-ba194c86e72f/httpwebrequest-times-out-even-when-transfer-is-ongoing?forum=ncl" rel="nofollow noreferrer">Alexander Sun in his question at MSDN</a> where an upload is aborted when it takes longer than the time specified in HttpWebRequest.Timeout -- which is kind of unexpected. We think similar to gouravmukherjee who writes in the forum:</p>
  <blockquote>
  <p>What I am trying to say is that the .net framework seems to close the connection after the default timeout period. I am not saying that I need to change this timeout, my point is why should the request be timed out or cancelled at all when the client is still sending data. I can understand that a timeout may happen when the connection is idle, but if there is data transfer going on, it should not be stopped forcibly.</p>
  </blockquote>
  <p>He also goes further in a later post:</p>
  <blockquote>
  <p>I spoke to MS about this and was told that this timeout is supposed keep the thread from hanging if the upload took too long. .net apparently does not care for the activity on the socket and cares only for the timeout. This is why uploading the the file stops abruptly.</p>
  <p>I feel this is a bug that the framework doesn't care for activity on the socket, what do you think?</p>
  </blockquote>
  <p>Our test code is very simple:</p>
  <pre><code>string uri = &quot;http://192.168.2.4:8080&quot;;
  HttpWebRequest request = HttpWebRequest.CreateDefault(new Uri(uri)) as HttpWebRequest;
  request.Method = &quot;POST&quot;;
  var bytes = Encoding.UTF8.GetBytes(&quot;Test content\n&quot;);
  int repeat = 500000; // upload much data to take longer than request.Timeout
  request.ContentLength = bytes.Length * repeat;
  request.Timeout = 1000; // 1 second
  var stream = request.GetRequestStream();
  for (int i = 0; i &lt; repeat; ++i)
      stream.Write(bytes, 0, bytes.Length); //exception is thrown here
  stream.Close();
  var response = request.GetResponse();
  </code></pre>
  <p>This results in the following exception:</p>
  <pre><code>Unhandled Exception: System.Net.WebException: The request was aborted: The request was canceled.
      at System.Net.ConnectStream.InternalWrite(Boolean async, Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
      at System.Net.ConnectStream.Write(Byte[] buffer, Int32 offset, Int32 size)
      at httptest.MainClass.Main(String[] args) in c:\Users\work\Desktop\httptest\httptest\Program.cs:line 27
  </code></pre>
  <p>Interestingly the error does only happen with the Microsoft Runtime. When using Mono everything works as expected: While writing to the stream, the timeout measurement is stopped.</p>
  <p>We wonder how to work around this problem on the Microsoft Runtime. Is it ok to set Timeout to infinity? We feel that this is not a good option. Or is there no other way than to switch to a asynchronous implementation. Since we run on a separate thread anyway, we would prefer the simple synchronous API.</p>