title: Webclient.UploadString gets System.Net.WebException:ReceiveFailure on subsequent call
body: <p>I've written a .net 3.5 console application that fetch some data from a webpage. When debug and run on my Windows 7 development machine, it works fine. </p>
  
  <p>But when it run on my hosted VPS running Debian 6.0.5 and Mono 2.6.7, it throws the following exception on 2nd or later call to <code>UploadString()</code></p>
  
  <pre><code>System.Net.WebException: Error getting response stream (ReadDone2): ReceiveFailure ---&gt; System.Exception:    at System.Net.WebConnection.HandleError(WebExceptionStatus st, System.Exception e, System.String where)
     at System.Net.WebConnection.ReadDone(IAsyncResult result)
     at System.Net.Sockets.Socket+SocketAsyncResult.Complete()
     at System.Net.Sockets.Socket+Worker.Receive()
    at System.Net.WebConnection.HandleError (WebExceptionStatus st, System.Exception e, System.String where) [0x00000] in &lt;filename unknown&gt;:0
    --- End of inner exception stack trace ---
    at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in &lt;filename unknown&gt;:0
    at System.Net.HttpWebRequest.GetResponse () [0x00000] in &lt;filename unknown&gt;:0
    at System.Net.WebClient.GetWebResponse (System.Net.WebRequest request) [0x00000] in &lt;filename unknown&gt;:0
    at System.Net.WebClient.ReadAll (System.Net.WebRequest request, System.Object userToken) [0x00000] in &lt;filename unknown&gt;:0
    at System.Net.WebClient.UploadDataCore (System.Uri address, System.String method, System.Byte[] data, System.Object userToken) [0x00000] in &lt;filename unknown&gt;:0
    at System.Net.WebClient.UploadData (System.Uri address, System.String method, System.Byte[] data) [0x00000] in &lt;filename unknown&gt;:0
  </code></pre>
  
  <p>My code is pretty straight forward (I have a try/catch/finally that dispose the webclient once done)</p>
  
  <pre><code>client = new WebClient();
  client.Proxy = null;
  client.Credentials = new NetworkCredential("xxx", "xxx");
  client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
  string postString = "";
  foreach (KeyValuePair&lt;string, string&gt; pair in postData)
  {
      postString += pair.Key + "=" + pair.Value + "&amp;";
  }
  string content = client.UploadString(url, postString);
  return content;
  </code></pre>
  
  <p>The above code is wrapped in a method, and is called many times for a list of data to be fetched. First call is always successful. On 2nd call onward, <code>UploadString()</code> will block for a good 30 seconds followed by the exception above. I notice this by watching the console output. If I left it running in a loop, after a few tries it will succeed and then fail again repeatedly.</p>
  
  <p>I have another method which has similar code, but calling <code>OpenRead()</code> instead and return the content. This runs without any issues on my VPS. Also there's 2 PHP5 web application hosted on the VPS, running without any issue.</p>
  
  <p>Since my VPS doesn't support X11 and I've no idea how to debug the code remotely using VS2010 (Or I should be using MonoDevelop). I'm hoping someone who had encountered a similar problem before has an answer to this, or some guide to debug this similar setup.</p>
  
  <p>There's a similar question, <a href="https://stackoverflow.com/questions/9454984/webclient-error-getting-response-stream">WebClient “error getting response stream</a>  but the answer is not helpful.
  Thanks</p>
  
  <p>EDIT: It slipped my mind that I could run MonoDevelop and debug on my development machine. Will do that now.</p>
  
  <p>EDIT: Trying to debug using MonoDevelop + Mono 2.6.7 on Windows produces a diff set of problems, MySQL.Data unable to connect to server and WebClient gives timeout error.</p>
  
  <p>I've resort to using HttpWebRequest and I get a similar exception when running on the VPS but not on my machine</p>
  
  <pre><code>System.Net.WebException: Error getting response stream (ReadDone2): ReceiveFailure ---&gt; System.Exception:    at System.Net.WebConnection.HandleError(WebExceptionStatus st, System.Exception e, System.String where)
     at System.Net.WebConnection.ReadDone(IAsyncResult result)
     at System.Net.Sockets.Socket+SocketAsyncResult.Complete()
     at System.Net.Sockets.Socket+Worker.Receive()
    at System.Net.WebConnection.HandleError (WebExceptionStatus st, System.Exception e, System.String where) [0x00000] in &lt;filename unknown&gt;:0
    --- End of inner exception stack trace ---
    at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x00000] in &lt;filename unknown&gt;:0
    at System.Net.HttpWebRequest.GetResponse () [0x00000] in &lt;filename unknown&gt;:0
  </code></pre>
  
  <p>HttpWebRequest is actually used in within WebClient, so getting the same exception seems reasonable.</p>
  
  <p>EDIT: After being able to compile Mono 2.10.8 and run with it, I'm getting a different exception. Not sure if it's related to <a href="http://en.wikipedia.org/wiki/Chunked_transfer_encoding" rel="nofollow noreferrer">Chunked transfer encoding</a> but again, no such error when I debug in windows.</p>
  
  <pre><code>System.Net.WebException: An error occurred performing a WebClient request. ---&gt; System.Net.ProtocolViolationException: The number of bytes to be written is greater than the specified ContentLength.
    at System.Net.WebConnectionStream.CheckWriteOverflow (Int64 contentLength, Int64 totalWritten, Int64 size) [0x00000] in &lt;filename unknown&gt;:0
    at System.Net.WebConnectionStream.BeginWrite (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback cb, System.Object state) [0x00000] in &lt;filename unknown&gt;:0
    at System.Net.WebConnectionStream.Write (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in &lt;filename unknown&gt;:0
    at System.Net.WebClient.UploadDataCore (System.Uri address, System.String method, System.Byte[] data, System.Object userToken) [0x00000] in &lt;filename unknown&gt;:0
    at System.Net.WebClient.UploadData (System.Uri address, System.String method, System.Byte[] data) [0x00000] in &lt;filename unknown&gt;:0
    --- End of inner exception stack trace ---
    at System.Net.WebClient.UploadData (System.Uri address, System.String method, System.Byte[] data) [0x00000] in &lt;filename unknown&gt;:0
    at System.Net.WebClient.UploadData (System.Uri address, System.Byte[] data) [0x00000] in &lt;filename unknown&gt;:0
    at System.Net.WebClient.UploadData (System.String address, System.Byte[] data) [0x00000] in &lt;filename unknown&gt;:0
    at System.Net.WebClient.UploadString (System.String address, System.String data) [0x00000] in &lt;filename unknown&gt;:0
    at (wrapper remoting-invoke-with-check) System.Net.WebClient:UploadString (string,string)
  </code></pre>
  
  <p>EDIT: I'll get <code>ProtocolViolationException</code> if I'm using <code>WebClient</code> but I get <code>ReceiveFailure</code>, which is the same exception as earlier if I'm using HttpWebRequest, all on Mono 2.10.8</p>