FluentFTP GetListing 没有 return 任何结果
FluentFTP GetListing doesn't return any result
我似乎无法从 FTP 服务器获取任何列表。 (FileZilla 正在显示目录和文件)。
我得到了这个代码:
FtpClient ftpConn = new FtpClient();
ftpConn.Host = FtpServer;
ftpConn.Port = FtpPort;
ftpConn.Credentials = new System.Net.NetworkCredential(Username, Password);
ftpConn.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
ftpConn.EncryptionMode = FtpEncryptionMode.Implicit;
ftpConn.ValidateCertificate += new FtpSslValidation(Client_ValidateCertificate);
ftpConn.BulkListing = false;
//ftpConn.DataConnectionType = FtpDataConnectionType.AutoPassive;
ftpConn.Connect();
FtpListItem[] FtpFolders = null;
FtpFolders = ftpConn.GetListing(Folder);
但是没用。我尝试了 FTP 选项,但没有得到任何结果。
还有更多建议吗?
根据您的代码,
由于无法连接到 FTP 服务器,您无法获得任何结果。
这是你错过的,参考这个:FAQ section of FluentFTP.
您可能有证书(*.crt, *.cer) 文件,将其放入您的源代码中,如下所示。
ftpConn.ClientCertificates.Add(new X509Certificate2(@"C:\ftpServer.crt"));
如果您的证书文件没有根链。
(例如,您自己制作的或私人证书文件)。
您需要在
添加更具体的代码
ftpConn.ValidateCertificate += new FtpSslValidation(Client_ValidateCertificate);
private void Client_ValidateCertificate(FtpClient control, FtpSslValidationEventArgs e)
{
if (e.PolicyErrors == SslPolicyErrors.None || e.Certificate.GetRawCertDataString() == "Use this condition for your situation")
{
e.Accept = true;
}
else
{
if (e.PolicyErrors == SslPolicyErrors.RemoteCertificateChainErrors)
{
//In this case, you need to choose connect or not. If your certificate file doen't have root chain.
}
else
{
//throw new Exception($"{e.PolicyErrors}{Environment.NewLine}{GetCertificateDetails(e.Certificate)}");
}
}
}
PS :如果您的 FTP 服务在 Windows 上,您别无选择,但如果它在 linux 或 unix 上工作,您可以使用 S FTP 与“Renci.SshNet.
更新:现在 windows 支持 openSSH,因此我们可以使用 sftp。
Installation of OpenSSH For Windows Server 2019 and Windows 10
我似乎无法从 FTP 服务器获取任何列表。 (FileZilla 正在显示目录和文件)。
我得到了这个代码:
FtpClient ftpConn = new FtpClient();
ftpConn.Host = FtpServer;
ftpConn.Port = FtpPort;
ftpConn.Credentials = new System.Net.NetworkCredential(Username, Password);
ftpConn.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
ftpConn.EncryptionMode = FtpEncryptionMode.Implicit;
ftpConn.ValidateCertificate += new FtpSslValidation(Client_ValidateCertificate);
ftpConn.BulkListing = false;
//ftpConn.DataConnectionType = FtpDataConnectionType.AutoPassive;
ftpConn.Connect();
FtpListItem[] FtpFolders = null;
FtpFolders = ftpConn.GetListing(Folder);
但是没用。我尝试了 FTP 选项,但没有得到任何结果。 还有更多建议吗?
根据您的代码,
由于无法连接到 FTP 服务器,您无法获得任何结果。
这是你错过的,参考这个:FAQ section of FluentFTP.
您可能有证书(*.crt, *.cer) 文件,将其放入您的源代码中,如下所示。
ftpConn.ClientCertificates.Add(new X509Certificate2(@"C:\ftpServer.crt"));
如果您的证书文件没有根链。
(例如,您自己制作的或私人证书文件)。
您需要在
ftpConn.ValidateCertificate += new FtpSslValidation(Client_ValidateCertificate);
private void Client_ValidateCertificate(FtpClient control, FtpSslValidationEventArgs e)
{
if (e.PolicyErrors == SslPolicyErrors.None || e.Certificate.GetRawCertDataString() == "Use this condition for your situation")
{
e.Accept = true;
}
else
{
if (e.PolicyErrors == SslPolicyErrors.RemoteCertificateChainErrors)
{
//In this case, you need to choose connect or not. If your certificate file doen't have root chain.
}
else
{
//throw new Exception($"{e.PolicyErrors}{Environment.NewLine}{GetCertificateDetails(e.Certificate)}");
}
}
}
PS :如果您的 FTP 服务在 Windows 上,您别无选择,但如果它在 linux 或 unix 上工作,您可以使用 S FTP 与“Renci.SshNet.
更新:现在 windows 支持 openSSH,因此我们可以使用 sftp。 Installation of OpenSSH For Windows Server 2019 and Windows 10