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



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

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

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

发现了一个类似的问题[与426有关-中止连接][1]实现了添加配置设置的建议,但即使是这种力量也对我有效。

任何能够将文件上传到linux FTP(TLS enbable(的机构,请分享逻辑或让我知道在我的情况下到底需要做什么。任何需要启用或禁用的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((是连接中止的地方,我们得到错误,得到的响应给出426错误[1] :powershell ftps上传导致错误";DATA连接在没有ssl关闭的情况下终止";正在运行的关闭

以下是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.```


同样的问题,但对于powershell脚本来说:

powershell ftps上传导致错误";DATA连接在没有ssl关闭的情况下终止";正在运行的关闭

并且提供了一个变通方法,至少对于vsftpd服务器来说,可以添加到服务器选项中:

strict_ssl_read_eof=无

它会不断抱怨(在内部,在日志中,在服务器中(,但你也不会例外。存在相关的安全风险,尽管(检查链接(

最新更新