【问题标题】:Error : File partially Uploading to linux FTP using C# FTPwebRequest错误:文件部分上传到使用 C# FTPwebRequest 的 linux FTP
【发布时间】:2020-10-13 12:01:17
【问题描述】:

我们想将文件上传到 Ubuntu FTP 服务器。我们可以使用 FILE Zilla 将文件连接并上传到服务器。 FTP 服务器启用 SSL 并具有 TLS1.2。上传或连接证书在接受后显示,我们才能连接或上传文件。

需要有一个自动系统,可以在文件创建时将文件上传到 FTP。为此,我们正在开发一个可以上传文件的 .net C# 应用程序。我们能够连接到 FTP 并使用 C# FtpWebRequest 显示其中存在的文件。但是当将文件上传到 ubuntu FTP (vsFTP) (使用 C# FTPWebRequest )时,文件被部分上传,然后连接终止。此错误会发生异常“GetResponse - 远程服务器返回错误:(426) Connection closed; transfer aborted..”

已检查防火墙以确认是否是导致问题的原因,因此在防火墙外运行了应用程序,但仍然遇到相同的异常。添加了 TLS1.2 和 ServicePointManager.ServerCertificateValidationCallback 的代码,但仍然无法获取上传成功响应的文件。

发现了一个类似的问题 [与 426- Abort connection 有关][1] 实施了添加配置设置的建议,但即使这对我也有用。

任何能够将文件上传到支持 TLS 的 linux FTP 的机构,请您分享逻辑或让我知道在我的情况下到底需要做什么。 任何需要启用或禁用的 FTP 配置设置都可以帮助我解决这个问题

只是为了确认何时禁用 SSL 并将其保持为普通 FTP,我们能够毫无问题地上传文件。

下面是上传文件的代码块

 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();

FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 行是连接中止的地方,我们得到错误 get response 给出 426 错误 [1]:powershell ftps upload causing error "DATA connection terminated without ssl shutdown" on stream close

下面是我们在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.```


  

【问题讨论】:

  • FTP 有文本模式和二进制模式。错误的一个原因是您试图以文本模式发送二进制数据。你说上传开始所以如果你在中间终止然后尝试更改为二进制模式。
  • 感谢 jdweng 的回复。我已将代码添加到 usebinary 以上传文件。
  • 我刚刚使用用于上传文件的代码块更新了我的初始帖子。
  • 如果您使用 SSL/TLS,那么您是安全的,应该使用带有 FTPS(而不是 FTP)的 URL。
  • okk .. 在 fileZilla 中,我使用协议作为 FTP 并且加密作为 ** Use Explicit Ftp over TLS is available* 所以我在代码中使用相同的。我在 TLS 中找到的大部分代码都将其用作 FTP。让我看看如何使用 FTPS

标签: c# linux file-upload ftpwebrequest vsftpd


【解决方案1】:

同样的问题,但这里是一个强大的 shell 脚本:

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

...并且至少为 vsftpd 服务器提供了一种解决方法,以将其添加到服务器选项中:

strict_ssl_read_eof=否

它会一直抱怨(在内部,在日志中,在服务器中),但你不会得到任何例外。但存在相关的安全风险(请查看链接)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    相关资源
    最近更新 更多