简体   繁体   English

错误:文件部分使用 C# FTPwebRequest 上传到 linux FTP

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

We want to upload files to a Ubuntu FTP server.我们想将文件上传到 Ubuntu FTP 服务器。 We are able to connect and upload the file to the server using the FILE Zilla.我们能够使用 FILE Zilla 连接并将文件上传到服务器。 The FTp server is SSL enabled and has TLS1.2. FTP 服务器启用了 SSL 并具有 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.需要有一个自动化系统,可以在创建文件时将文件上传到 FTP。 For this we are developing a .net C# application which woud upload the file.为此,我们正在开发一个 .net C# 应用程序,它可以上传文件。 we are able to connect to the FTP and also display the files present in it with C# FtpWebRequest.我们能够连接到 FTP 并使用 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.但是当将文件上传到 ubuntu FTP(vsFTP)(使用 C# FTPWebRequest )时,文件被部分上传,然后连接终止。 An exception occurs with this error "GetResponse - The remote server returned an error: (426) Connection closed; transfer aborted.." .出现此错误的异常“GetResponse - 远程服务器返回错误:(426) 连接关闭;传输中止..”

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.添加了 TLS1.2 和 ServicePointManager.ServerCertificateValidationCallback 的代码,但仍然无法通过成功响应上传文件。

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.发现了一个类似的问题 [与 426-中止连接相关][1] 实现了添加配置设置的建议,但即使是这个 dint 也对我有用。

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.任何能够将文件上传到支持 TLS 的 linux FTP 的机构,请您分享逻辑或让我知道在我的情况下到底需要做什么。 Any FTP config setting that needs to enabled or disabled would help me fix this任何需要启用或禁用的 FTP 配置设置都会帮助我解决这个问题

Just to confirm when disabled the SSL and kept it as plain FTp we are able to upload the File without any issue.只是为了确认何时禁用 SSL 并将其保留为普通 FTp,我们可以毫无问题地上传文件。

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();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是连接中止的地方,我们得到错误 get 响应给出了 426 错误 [1]: powershell ftps 上传导致错误“数据连接终止而没有 ssl 关闭”在流关闭时

Below is the log we get when the ftp fails.下面是我们在 ftp 失败时得到的日志。

   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.```


  

The same problem, but for a power shell script is here:同样的问题,但对于 power shell 脚本,这里是:

powershell ftps upload causing error "DATA connection terminated without ssl shutdown" on stream close powershell ftps上传导致流关闭时出现错误“数据连接终止而ssl关闭”

... and a workaround is offered, at least for the vsftpd server, to add, to the server options: ...并且提供了一种解决方法,至少对于 vsftpd 服务器,添加到服务器选项中:

strict_ssl_read_eof=NO strict_ssl_read_eof=否

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)但是,存在相关的安全风险(检查链接)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用C#ftpwebrequest将xml文件ftp到Zebra打印机时出错 - Error trying to use C# ftpwebrequest to ftp xml file to Zebra printer 有没有一种方法可以使用FtpWebRequest使用C#中的客户端证书对FTP进行身份验证? - Is there a way to use FtpWebRequest to authenticate to FTP using client certificates in C#? 在FTP服务器上尚不存在文件时,使用FtpWebRequest通过FTP上传文件时出现问题 - Problem Uploading a file via FTP using FtpWebRequest when the file does not already exist on FTP server 在C#中使用FtpWebRequest和BackgroundWorker递归上传文件 - Recursive uploading files using FtpWebRequest , BackgroundWorker in C# 远程服务器返回错误:(550) on upload file to FTP using FtpWebRequest - The remote server returned an error: (550) on upload file to FTP using FtpWebRequest 将文件上传到FTP服务器C#时出错 - Error while Uploading file to FTP server C# 将文本文件上传到FTP服务器C#时出错 - Error while uploading a text file to FTP server C# 使用 C# 将文件通过 FTP 文件传输到包含数据集的大型机 - 将 FTP 脚本转换为 FtpWebRequest 代码 - Use C# to FTP file to mainframe including dataset - Translate FTP script to FtpWebRequest code C#FtpWebRequest错误403 - C# FtpWebRequest Error 403 c#-在FTP上上传文件无法正常工作 - c# - uploading file on FTP is not working properly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM