title: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host
body: <p>When I transfer a file about 50Mb over HTTP <strong>sometimes</strong> I get this error:</p>
  
  <p><strong>Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.</strong></p>
  
  <p>First of all I couldn't find any solution under <a href="http://stackoverflow.com">http://stackoverflow.com</a>.</p>
  
  <p>Any clue what I have to improve/change? </p>
  
  <p>app.config:</p>
  
  <pre><code> &lt;bindings&gt;
        &lt;basicHttpBinding&gt;
          &lt;binding name="BasicHttpBinding_MyDomainServicesoap" closeTimeout="00:03:00" openTimeout="00:04:00" receiveTimeout="00:10:00" sendTimeout="00:05:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="50000000" maxBufferPoolSize="50000000" maxReceivedMessageSize="50000000" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"&gt;
            &lt;readerQuotas maxDepth="64" maxStringContentLength="16384" maxArrayLength="32768" maxBytesPerRead="8192" maxNameTableCharCount="16384"/&gt;
            &lt;security mode="None"&gt;
              &lt;transport clientCredentialType="None" proxyCredentialType="None" realm=""/&gt;
              &lt;message clientCredentialType="UserName" algorithmSuite="Default"/&gt;
            &lt;/security&gt;
          &lt;/binding&gt;
          &lt;binding name="BasicHttpBinding_MyAuthenticationDomainServicesoap" closeTimeout="00:03:00" openTimeout="00:04:00" receiveTimeout="00:10:00" sendTimeout="00:05:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="50000000" maxBufferPoolSize="50000000" maxReceivedMessageSize="50000000" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"&gt;
            &lt;readerQuotas maxDepth="64" maxStringContentLength="16384" maxArrayLength="32768" maxBytesPerRead="8192" maxNameTableCharCount="16384"/&gt;
            &lt;security mode="None"&gt;
              &lt;transport clientCredentialType="None" proxyCredentialType="None" realm=""/&gt;
              &lt;message clientCredentialType="UserName" algorithmSuite="Default"/&gt;
            &lt;/security&gt;
          &lt;/binding&gt;
        &lt;/basicHttpBinding&gt;
      &lt;/bindings&gt;
  </code></pre>
  
  <p>code:</p>
  
  <pre><code>private void FinishWebRequest(IAsyncResult result)
          {
              // Assign values to these objects here so that they can be referenced in the finally block
              Stream remoteStream = null;
              Stream localStream = null;
              WebResponse response = null;
  
              try
              {
                  response = request.EndGetResponse(result);
  
                if (response != null)
                  {
                      // Once the WebResponse object has been retrieved, get the stream object associated with the response's data
                      remoteStream = response.GetResponseStream();
  
                      // Create the local file
                      string pathToSaveFile = Path.Combine(FileManager.GetFolderContent(), FileView.Filename);
                      localStream = File.Create(pathToSaveFile);
  
                      WinAPI.SYSTEM_INFO sysinfo = new WinAPI.SYSTEM_INFO();
                      WinAPI.GetSystemInfo(ref sysinfo);
  
                      // Allocate a buffer 
                      byte[] buffer = new byte[int.Parse(sysinfo.dwPageSize.ToString())];     
                      int bytesRead;
  
                      // Simple do/while loop to read from stream until no bytes are returned
                      do
                      {
                          // Read data (up to 1k) from the stream
                          bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
  
                          // Write the data to the local file
                          localStream.Write(buffer, 0, bytesRead);
  
                          // Increment total bytes processed
                          BytesProcessed += bytesRead;
                      } while (bytesRead &gt; 0);
  
                      FileView.Downloaded = DateTime.Now;
  
                      if (BytesProcessed &gt; 0)
                      {
                          FileView.IsSuccess = true;
                          FileView.IsDownloading = false;
                      }
                      else
                      {
                          FileView.IsSuccess = false;
                          FileView.IsDownloading = false;
                      }
                  } 
              }
              catch (Exception ex)
              {
                  #region Error
                  LogEntry l = new LogEntry();
                  l.Message = string.Format("{0}", ex.Message);
                  l.Title = "FinishWebRequest() Error";
                  l.Categories.Add(Category.General);
                  l.Priority = Priority.Highest;
  
                  if (ex.InnerException != null) l.ExtendedProperties.Add("InnerException", ex.InnerException.Message);
  
                  CustomLogger.CustomLogger.WriteErrorLog(l);
                  #endregion
              }
              finally
              {
                  // Close the response and streams objects here to make sure they're closed even if an exception is thrown at some point
                  if (response != null) response.Close();
                  if (remoteStream != null) remoteStream.Close();
                  if (localStream != null) localStream.Close();
              }
          }
  </code></pre>