Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .net核心Ftp命令不';无法在我的生产服务器上工作且不会引发错误_C#_.net Core_Ftp - Fatal编程技术网

C# .net核心Ftp命令不';无法在我的生产服务器上工作且不会引发错误

C# .net核心Ftp命令不';无法在我的生产服务器上工作且不会引发错误,c#,.net-core,ftp,C#,.net Core,Ftp,我有一个c#.netcore 2.2控制台应用程序来测试ftp命令。 它在我的开发机器上工作得很好,但在我的生产服务器上却没有。 ftp命令已执行,但不会发生任何情况,也不会引发任何错误 以下是安装在我的生产服务器上的dotnet版本: 这是我的应用程序的代码 class Program { static async Task Main(string[] args) { var config = new FtpServerConfiguration

我有一个c#.netcore 2.2控制台应用程序来测试ftp命令。 它在我的开发机器上工作得很好,但在我的生产服务器上却没有。 ftp命令已执行,但不会发生任何情况,也不会引发任何错误

以下是安装在我的生产服务器上的dotnet版本:

这是我的应用程序的代码

    class Program
{
    static async Task Main(string[] args)
    {
        var config = new FtpServerConfiguration
        {
            Login = "badlogin", 
            Password = "abcd1234", 
            Server = "ftp.myserver.com" 
        };

        Console.WriteLine("First Attempt with bad login");
        var results = await GetFileList(config, "");

        Console.WriteLine("\nSecond Attempt with good login");
        config.Login = "goodlogin";
        results = await GetFileList(config, "");

        Console.ReadLine();
    }


    private static async Task<IEnumerable<string>> GetFileList(FtpServerConfiguration config, string remoteDir)
    {
        IList<string> files = new List<string>();
        try
        {
            var request = (FtpWebRequest)WebRequest.Create(string.Concat("ftp://", config.Server, "/", remoteDir));
            request.KeepAlive = true;
            request.Method = WebRequestMethods.Ftp.ListDirectory;
            request.UseBinary = true;
            request.UsePassive = config.UsePassive;
            request.EnableSsl = false;
            request.Timeout = config.Timeout;
            request.Credentials = new NetworkCredential(config.Login, config.Password);

            Console.WriteLine("send command");
            using (var response = await request.GetResponseAsync())
            {
                Console.WriteLine("response received");
                var reader = new StreamReader(response.GetResponseStream());
                string line = reader.ReadLine();
                while (line != null)
                {
                    files.Add(line);
                    line = reader.ReadLine();
                }
            }
            Console.WriteLine("Files :");
            foreach (var file in files)
            {
                Console.WriteLine("\t" + file);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("ERROR: "+ex.Message);
        }

        return files;
    }
}
public class FtpServerConfiguration
{
    public string Server { get; set; }

    public string Login { get; set; }

    public string Password { get; set; }

    public bool UsePassive { get; set; }

    public int Timeout { get; set; } = 10000;
}
类程序
{
静态异步任务主(字符串[]args)
{
var config=新的FtpServerConfiguration
{
Login=“badlogin”,
Password=“abcd1234”,
Server=“ftp.myserver.com”
};
Console.WriteLine(“首次尝试登录错误”);
var results=await GetFileList(config,“”);
Console.WriteLine(“\n第二次尝试,登录良好”);
config.Login=“goodlogin”;
结果=等待GetFileList(配置“”);
Console.ReadLine();
}
专用静态异步任务GetFileList(FtpServerConfiguration配置,字符串remoteDir)
{
IList files=新列表();
尝试
{
var request=(FtpWebRequest)WebRequest.Create(string.Concat(“ftp://”,config.Server,“/”,remoteDir));
request.KeepAlive=true;
request.Method=WebRequestMethods.Ftp.ListDirectory;
request.UseBinary=true;
request.usesponsive=config.usesponsive;
request.enablesl=false;
request.Timeout=config.Timeout;
request.Credentials=newnetworkcredential(config.Login,config.Password);
Console.WriteLine(“发送命令”);
使用(var response=wait request.GetResponseAsync())
{
Console.WriteLine(“收到响应”);
var reader=newstreamreader(response.GetResponseStream());
字符串行=reader.ReadLine();
while(行!=null)
{
文件。添加(行);
line=reader.ReadLine();
}
}
Console.WriteLine(“文件:”);
foreach(文件中的var文件)
{
Console.WriteLine(“\t”+文件);
}
}
捕获(例外情况除外)
{
Console.WriteLine(“错误:+ex.Message”);
}
归还文件;
}
}
公共类FtpServerConfiguration
{
公共字符串服务器{get;set;}
公共字符串登录{get;set;}
公共字符串密码{get;set;}
公共布尔值{get;set;}
公共int超时{get;set;}=10000;
}

我从这两台机器上用相同的凭证查询相同的FTP服务器,下面是我的开发机器上的结果

然后在我的生产服务器上

有人知道发生了什么事吗? 谢谢你

谢谢@MartinPrikryl 通过查看FTP日志,我看到了来自这两台机器的调用之间的差异

因此,我尝试了一个新的调用,添加usesponsive=true,现在它可以在两台机器上工作


我唯一无法解释的是“为什么它不生成任何错误?”

您如何知道生产服务器上的默认文件夹中有任何要列出的文件?或者您的登录名具有查看它们的权限?图像链接不起作用。您可以使用在生产服务器上运行的任何(独立、GUI)FTP客户端列出该目录吗?显然,我从我的开发计算机或生产服务器FTP协议日志中查询具有相同凭据的同一FTP服务器。您等待的时间够长吗?它很可能在一段时间后引发超时异常。