0

I am trying to upload files on ftp with resume if internet connection is lost. It does resume file uploading 2-3 times if internet connection is lost but after that an exception is thrown that says "Server committed a protocol violation". Please review code given below and help me if you find any issues in code. I know the connection remains open somewhere in the code but I am unable to identify the place.

protected void upload_file(String server_path, string file_path)
{
    if (reqFTP != null)
        reqFTP.Abort();
    bool flag = false;
    FileInfo fileInf = new FileInfo(file_path);
    String file_name = fileInf.Name;
    String ftpusername = "aaaa";
    String ftppassword = "Aas123";
    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp" + file_name));
        reqFTP.Credentials = new System.Net.NetworkCredential(ftpusername, ftppassword);
        reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
        reqFTP.KeepAlive = false;
        FtpWebResponse response;
        try
        {
            long size = 0;
            using (response = (FtpWebResponse)reqFTP.GetResponse())
            {
                size = response.ContentLength;

            }
            response.Close();
            reqFTP.Abort();
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp" + file_name));
            reqFTP.Credentials = new System.Net.NetworkCredential(ftpusername, ftppassword);
            reqFTP.Method = System.Net.WebRequestMethods.Ftp.AppendFile;
            reqFTP.KeepAlive = false;
            int b = 0;
            System.IO.FileInfo fi = new System.IO.FileInfo(file_path);
            int bytes = 0;
            System.IO.FileStream fs = new FileStream(file_path, FileMode.Open, FileAccess.Read);
            fs.Seek(size, SeekOrigin.Begin);
            long total_bytes = fi.Length - size;
            byte[] buffer = new byte[8092]; 
            using (System.IO.Stream rs = reqFTP.GetRequestStream())
            {
                while ((bytes = fs.Read(buffer, 0, buffer.Length)) != 0)
                {
                    try
                    {
                        rs.Write(buffer, 0, bytes);
                    }
                    catch (Exception e1)
                    {
                        MessageBox.Show(e1.Message);
                        while (!checkForInternetConnection()) ;
                        upload_file(server_path, file_path);
                    }

                }
            }
            fs.Close();

            using (FtpWebResponse uploadResponse = (FtpWebResponse)reqFTP.GetResponse())
            {
                var value = uploadResponse.StatusDescription;
                MessageBox.Show(value);
                uploadResponse.Close();
            }



        }
        catch (WebException ex)
        {
            MessageBox.Show(ex.Message);
            while (!checkForInternetConnection()) ;
            FtpStatusCode status;
            using (response = (FtpWebResponse)ex.Response)
            {
                status = (FtpStatusCode)response.StatusCode;                        
            }
            if (status == FtpStatusCode.ActionNotTakenFileUnavailable)
            {
                MessageBox.Show("file not found");
                try
                {
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp" + file_name));
                    reqFTP.Credentials = new System.Net.NetworkCredential(ftpusername, ftppassword);
                    reqFTP.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
                    reqFTP.KeepAlive = false;
                    int b = 0;

                    System.IO.FileInfo fi = new System.IO.FileInfo(file_path);
                    int bytes = 0;
                    System.IO.FileStream fs = new FileStream(file_path, FileMode.Open, FileAccess.Read);
                    long total_bytes = fi.Length;

                    byte[] buffer = new byte[8092]; 
                    using (System.IO.Stream rs = reqFTP.GetRequestStream())
                    {
                        while ((bytes = fs.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            try
                            {
                                rs.Write(buffer, 0, bytes);
                            }
                            catch
                            {
                                while (!checkForInternetConnection()) ;
                                upload_file(server_path, file_path);
                            }

                        }
                    }

                    fs.Close();
                    using (FtpWebResponse uploadResponse = (FtpWebResponse)reqFTP.GetResponse())
                    {
                        var value = uploadResponse.StatusDescription;
                        MessageBox.Show(value);
                    }
                }
                catch (WebException ex1)
                {
                    MessageBox.Show("catch it");
                    while (!checkForInternetConnection()) ;
                    upload_file(server_path, file_path);

                }
            }
            else
            {
                while (!checkForInternetConnection()) ;
                upload_file(server_path, file_path);
            }


        }

    }

I have tried

    reqFTP.KeepAlive = true

but that does not work.

  • 2
    Possible duplicate of [The underlying connection was closed: The server committed a protocol violation. FTP](http://stackoverflow.com/questions/9815919/the-underlying-connection-was-closed-the-server-committed-a-protocol-violation) – stuartd Dec 16 '15 at 10:24
  • I have tested the link you have provided but that does not work that's why I post my own question because things are a bit different in my code – Muhammad Yaseen Dec 16 '15 at 10:30
  • 1
    It helps if you include what you have tried in your question. – stuartd Dec 16 '15 at 10:49
  • https://drive.google.com/file/d/0B5oml0q1E5dHMWZjR2tjclFDTDQ/view?usp=sharing @MartinPrikryl please review the file and tell me if you could find the issue from it – Muhammad Yaseen Dec 16 '15 at 12:32
  • Possibly this issue: [Can connect to FTP using FileZilla or WinSCP, but not with FtpWebRequest or FluentFTP](https://stackoverflow.com/q/65636482/850848). – Martin Prikryl Jan 07 '22 at 07:17

0 Answers0