访问FTP服务器时,未知命令FEAT

本文关键字:未知 命令 FEAT FTP 服务器 访问 | 更新日期: 2023-09-27 17:50:53

我刚刚根据这里提供的代码创建了一个FTP服务器http://www.codeguru.com/csharp/csharp/cs_network/sockets/article.php/c7409/A-C-FTP-Server.htm。然后我为FtpClient安装了Nuget包,这样我就可以创建一个连接到FTP服务器的测试FTP客户端。下面是我的代码:

[TestMethod]
    async public Task TestMethod1() // wanted to make it async as I may connect have something related to database in future
    {
        var client = new FtpClient
        {
            Host = "127.0.0.1",
            Port = 21,
            Credentials = new NetworkCredential
            {
                UserName = "123",
                Password = "123",
            },
            DataConnectionType = FtpDataConnectionType.PASV // If I don't add this, I get EPSV unknown command
        };
        await client.ConnectAsync();
        Debug.WriteLine("Connected");
        client.SetWorkingDirectory(@"C:'Users'myname'Desktop");
        Debug.WriteLine(client.GetWorkingDirectory());
        try
        {
            var items = client.GetListing(); // Exception shows up here.
            Debug.WriteLine(items.Count());
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
}

问题可能是我的服务器中没有用于FEAT的命令处理程序。但是,我的FTP服务器运行filezilla完全没问题。如果命令处理程序是必需的,有人能为我提供这个代码吗?

如果我忽略这个错误,我在测试方法中得到这个异常"无法从传输连接读取数据:一个现有的连接被远程主机强制关闭。"

任何想法?

[编辑]下面是完整的异常:

Test method Blackhawk.Core.FtpServer.Test.UnitTest1.TestMethod1 threw exception: 
System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. --->        System.Net.Sockets.SocketException: An existing connection was forcibly closed   by the remote host
    at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
   at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
 --- End of inner exception stack trace ---
     at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
   at    System.Net.FtpClient.FtpSocketStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.FtpClient.FtpSocketStream.ReadLine(Encoding encoding)
   at System.Net.FtpClient.FtpClient.GetReply()
   at System.Net.FtpClient.FtpClient.Execute(String command)
   at System.Net.FtpClient.FtpClient.OpenPassiveDataStream(FtpDataConnectionType type, String command, Int64 restart)
   at System.Net.FtpClient.FtpClient.OpenDataStream(String command, Int64 restart)
   at System.Net.FtpClient.FtpClient.GetListing(String path, FtpListOption options)
   at System.Net.FtpClient.FtpClient.GetListing(String path)
   at Blackhawk.Core.FtpServer.Test.UnitTest1.<TestMethod1>d__2.MoveNext() in UnitTest1.cs: line 44
 --- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

访问FTP服务器时,未知命令FEAT

我可以通过将数据连接类型更改为AutoActive来解决这个问题。下面是最终有效的代码:

[TestMethod]
    async public Task FtpTestUser1()
    {
        var client = new FtpClient
        {
            Host = "127.0.0.1",
            Port = 21,
            Encoding = System.Text.Encoding.Default,
            Credentials = new NetworkCredential
            {
                // Storing username, password and default directory in a .dat file
                UserName = "123", 
                Password = "123",
            },
            DataConnectionType = FtpDataConnectionType.AutoActive // Changed this part.
        };
        await client.ConnectAsync();
        Debug.WriteLine("Connected");
        var path = @"Test"; // This path is now relative to the default directory
        client.SetWorkingDirectory(path);
        Debug.WriteLine(client.GetWorkingDirectory());

        var items = client.GetListing();
        Assert.IsTrue(items.Any());
        Debug.WriteLine(items.Count());
        foreach (var ftpListItem in items)
        {
            Debug.WriteLine(string.Format("File name is {0}", ftpListItem.Name));
        }        
    }