通过SSL访问ftp服务器上的目录时出现FTP错误

问题描述 投票:0回答:1

我已经在C#.Net 4.5中开发了一个Web服务,该服务连接到ftp服务器(使用SSL)并列出存储在ftp服务器目录中的所有文件。该代码去年(2019年开始或2018年底)运行良好,但由于我三周前再次测试该代码,因此不再有效。我尝试了很多事情:

-我将目标框架从4.5更改为4.8(Link of the article

-使用fluentFTP nuget包(但我有同样的错误

事实是,我可以使用Filezilla连接到ftp服务器并访问目录而没有任何错误(因此,我认为这不是防火墙问题)我检查了我的计算机和ftp服务器之间的ftp交换日志,并且在ftp命令MLSD->打开目录列表“目录”(来自服务器的最后一条消息-> .Net错误)的数据通道期间发生了错误。失败,因为远程方已关闭传输流”

这里是代码:

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://urlFtpServer:21/directory");
            request.Method = WebRequestMethods.Ftp.ListDirectory;
            request.EnableSsl = true;
            // Sets the user login and password.  
            request.Credentials = new NetworkCredential("login", "password");

            request.KeepAlive = true;

            try
            {
                // Send the request.
                using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            IEnumerable<string> lstDirectoryFiles = reader.ReadToEnd()
                                                                   .Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

                            // Use the user criteria to get only the file needed.
                            if (string.IsNullOrEmpty(in_searchPattern))
                                return lstDirectoryFiles.ToList();

                            Regex rgx = new Regex(in_searchPattern);

                            return lstDirectoryFiles.Where(st => rgx.IsMatch(st)).ToList();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //Here is the exception: authentication failed because the remote party has closed the transport stream
            }

请帮助:)

c# ftpwebrequest ftps .net-4.8
1个回答
0
投票

可能是由于FTP服务器的TLS支持。

© www.soinside.com 2019 - 2024. All rights reserved.