Error : File partially Uploading to linux FTP using C# FTPwebRequest

904 views Asked by At

We want to upload files to a Ubuntu FTP server. We are able to connect and upload the file to the server using the FILE Zilla. The FTp server is SSL enabled and has TLS1.2. While uploading or connecting the certificate is shown after accepting it only are we able to connect or upload the file.

Need to have a automated system which would upload the files to the FTP as and when the files are created. For this we are developing a .net C# application which woud upload the file. we are able to connect to the FTP and also display the files present in it with C# FtpWebRequest. But when uploading a file to the ubuntu FTP (vsFTP) ( using C# FTPWebRequest ) the file gets partially uploaded and then the connection terminates. An exception occurs with this error "GetResponse - The remote server returned an error: (426) Connection closed; transfer aborted..".

Have checked the Firewall to confirm if that is causing the issue, so ran the application outside the firewall but still get the same exception. Added the code for TLS1.2 and also ServicePointManager.ServerCertificateValidationCallback but still not able to get the file uploaded with success response.

found a similar question [related to 426- Abort connection][1] implemented the suggestion for adding a config setting, but even this dint work for me.

Any body who has been able to upload a file to linux FTP which is TLS enbable would you please share the logic or let me know what exactly is need to be done in my case. Any FTP config setting that needs to enabled or disabled would help me fix this

Just to confirm when disabled the SSL and kept it as plain FTp we are able to upload the File without any issue.

Below is the Code Block used to upload the file

 FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://siteURl//home/Files/" + "Myfile.xml");
            request.Credentials = new NetworkCredential("UserName", "Password");
            request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.CacheIfAvailable);
            request.Method = WebRequestMethods.Ftp.UploadFile;
            //request.KeepAlive = false;
            request.Timeout = 10000;
            request.UseBinary = true;
            request.UsePassive = true;
            request.KeepAlive = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            request.EnableSsl = true;
            
            ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
            
            StreamReader sourceStream = new StreamReader(filePath);
            byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();
            request.ContentLength = fileContents.Length;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();
          FtpWebResponse response = (FtpWebResponse)request.GetResponse();

The line FtpWebResponse response = (FtpWebResponse)request.GetResponse(); is where the connection aborts and we get the error the get response gives the 426 error [1]: powershell ftps upload causing error "DATA connection terminated without ssl shutdown" on stream close

Below is the log we get when the ftp fails.

   ProcessId=4236
   DateTime=2020-10-09T10:46:50.5147134Z
System.Net Information: 0 : [7256] FtpControlStream#62468121 - Received response [426 Failure reading network stream.]
   ProcessId=4236
   DateTime=2020-10-09T10:46:50.5157116Z
System.Net.Sockets Verbose: 0 : [7256] Entering Socket#49652976::Dispose()
   ProcessId=4236
   DateTime=2020-10-09T10:46:50.5177039Z
System.Net Information: 0 : [7256] FtpWebRequest#66824994::(Releasing FTP connection#62468121.)
   ProcessId=4236
   DateTime=2020-10-09T10:46:50.5177039Z
System.Net Verbose: 0 : [7256] Entering FtpWebRequest#66824994::GetResponse()
   ProcessId=4236
   DateTime=2020-10-09T10:46:50.5187110Z
System.Net Information: 0 : [7256] FtpWebRequest#66824994::GetResponse(Method=STOR.)
   ProcessId=4236
   DateTime=2020-10-09T10:46:50.5187110Z
System.Net Error: 0 : [7256] Exception in FtpWebRequest#66824994::GetResponse - The remote server returned an error: (426) Connection closed; transfer aborted..
  at System.Net.FtpWebRequest.GetResponse()
   ProcessId=4236
   DateTime=2020-10-09T10:46:50.5286972Z
System.Net Verbose: 0 : [7256] Exiting FtpWebRequest#66824994::GetResponse() 
   ProcessId=4236
   DateTime=2020-10-09T10:46:50.5286972Z
System.Net Information: 0 : [5524] ServicePoint#60068066 - Closed as idle.
   ProcessId=4236
   DateTime=2020-10-09T10:48:17.3365536Z
System.Net Information: 0 : [5524] ServicePoint#57712780 - Closed as idle.```


  

1

There are 1 answers

0
jaime On

The same problem, but for a power shell script is here:

powershell ftps upload causing error "DATA connection terminated without ssl shutdown" on stream close

... and a workaround is offered, at least for the vsftpd server, to add, to the server options:

strict_ssl_read_eof=NO

It will keep complaining (internally, in the log, the server), but you'll get no exception. There are security risks associated, though (check the link)