How to fix error: Unable to read data from the transport connection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ktxn

    #1

    How to fix error: Unable to read data from the transport connection

    I encountered the above error and don't know how to fix it or set the timer. Anyone please help.

    Here is my code for the background thread:

    Code:
    private BackgroundWorker _backgroundWorker = null;
    Stream strResponse;
    Stream strLocal;
    HttpWebRequest webRequest;
    HttpWebResponse webResponse;
    bool goPause = false;
    
    
    private void btnDownload_Click(object sender, EventArgs e)
    {
        // Let the user know we are connecting to the server
        lblProgress.Text = "Download Starting";
        long startPoint = 0;
    
        if (File.Exists(txtPath.Text))
            startPoint = new FileInfo(txtPath.Text).Length;
    
        int startPointInt = Convert.ToInt32(startPoint);
    
        List<Object> arguments = new List<Object>();
        arguments.Add(TxtURL.Text);
        arguments.Add(txtPath.Text);
        arguments.Add(startPointInt);
    
        _backgroundWorker = CreateWorker();
        _backgroundWorker.RunWorkerAsync(arguments);
    
    }
    
    public BackgroundWorker CreateWorker()
    {
        BackgroundWorker bg = new BackgroundWorker();
        bg.WorkerSupportsCancellation = true;
        bg.DoWork += DoWork;
        return bg;
    }
    
    private void DoWork(Object sender, DoWorkEventArgs e)
    {
        List<Object> arguments = (List<Object>)e.Argument;
        try
        {
            int startPointInt = Convert.ToInt32(arguments[2]);
    
            // Create a request to the file we are downloading
            webRequest = (HttpWebRequest)WebRequest.Create((String)arguments[0]);
                    
             webRequest.AddRange(startPointInt);
    
             webRequest.Credentials =CredentialCache.DefaultCredentials;
    
             // Retrieve the response from the server
             webResponse = (HttpWebResponse)webRequest.GetResponse();
    
              Int64 fileSize = webResponse.ContentLength;
    
              strResponse = webResponse.GetResponseStream();
    
              if (startPointInt == 0)
              {
                  strLocal = new FileStream((String)arguments[1], FileMode.Create, FileAccess.Write, FileShare.None);
               }
               else
               {
                  strLocal = new FileStream((String)arguments[1], FileMode.Append, FileAccess.Write, FileShare.None);
               }
    
                int bytesSize = 0;
    
                byte[] downBuffer = new byte[2048];
    
                while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
                {
                    strLocal.Write(downBuffer, 0, bytesSize);
    
                     if (goPause)
                         break;
                 }
                }
                finally
                {
                    goPause = false;
    
                    if (webRequest != null)
                        webRequest = null;
                    if (webResponse != null)
                        webResponse.Close();
                    if (strResponse != null)                
                        strResponse.Close();
                    if (strLocal != null)
                        strLocal.Close();                
                }
            }
    }
    
    private void btnStop_Click(object sender, EventArgs e)
    {
       // Close the web response and the streams
       if (webResponse != null)
       {
            goPause = true;
    
            _backgroundWorker.CancelAsync();
    
            webResponse.Close();
            strResponse.Close();
            strLocal.Close();
        }
    }
Working...