使用 WinSCP .NET 程序集从 FTP 下载文件的代码无法连接,但我可以在 WinSCP GUI 中连接
Code to download file from FTP with WinSCP .NET assembly does not connect, but I can connect in WinSCP GUI
我需要通过 WinSCP .NET 程序集从 FTP 下载文件。我目前有此代码,但错误提示身份验证失败。
try
{
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "172.xx.xxx.xx",
UserName = "usersample",
Password = "P@ssw0rd",
PortNumber = 21
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Download files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
transferResult =
session.GetFiles(
"/HST/sample.txt", "C:\Users\john\Documents\SampleFolder\",
false, transferOptions);
// Throw on any error
transferResult.Check();
// Print results
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Console.WriteLine("Download of {0} succeeded", transfer.FileName);
}
Console.ReadLine();
}
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e);
Console.ReadLine();
}
我从这个参考中得到了代码:https://winscp.net/eng/docs/library_session_getfiles
原来,它有以下内容:
SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
但我删除了它,因为我没有它,我认为它是给 SFTP 的(不确定)
我已经手动尝试了凭据并且它们有效。我能够登录。
请帮忙。谢谢。
除了 SSH 连接凭据之外,您还需要 SSH 主机密钥指纹。
You should get an SSH host key fingerprint along with your credentials
from a server administrator. Knowing the host key fingerprint and thus
being able to verify it is an integral part of securing an SSH
connection. It prevents man-in-the-middle attacks.
这可能会帮助您获得主机密钥:
针对您的情况有 WinSCP 常见问题解答:
Why I cannot connect/transfer using script, when I can using GUI (or vice versa)?
首先,最简单的解决方案是使用 GUI 会话中的 Generate transfer code 函数,以获得工作代码模板。
- 登录GUI;
- Select远程面板要下载的文件,本地面板要下载的目标本地目录;
- 使用下载命令;
- 在 Download transfer options dialog 上,使用 传输设置 > 生成代码;
Generate transfer code dialog 打开。 Select .NET 程序集选项卡 并确保选择 C# 语言。
我需要通过 WinSCP .NET 程序集从 FTP 下载文件。我目前有此代码,但错误提示身份验证失败。
try
{
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "172.xx.xxx.xx",
UserName = "usersample",
Password = "P@ssw0rd",
PortNumber = 21
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Download files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
transferResult =
session.GetFiles(
"/HST/sample.txt", "C:\Users\john\Documents\SampleFolder\",
false, transferOptions);
// Throw on any error
transferResult.Check();
// Print results
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Console.WriteLine("Download of {0} succeeded", transfer.FileName);
}
Console.ReadLine();
}
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e);
Console.ReadLine();
}
我从这个参考中得到了代码:https://winscp.net/eng/docs/library_session_getfiles
原来,它有以下内容:
SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
但我删除了它,因为我没有它,我认为它是给 SFTP 的(不确定) 我已经手动尝试了凭据并且它们有效。我能够登录。 请帮忙。谢谢。
除了 SSH 连接凭据之外,您还需要 SSH 主机密钥指纹。
You should get an SSH host key fingerprint along with your credentials from a server administrator. Knowing the host key fingerprint and thus being able to verify it is an integral part of securing an SSH connection. It prevents man-in-the-middle attacks.
这可能会帮助您获得主机密钥:
针对您的情况有 WinSCP 常见问题解答:
Why I cannot connect/transfer using script, when I can using GUI (or vice versa)?
首先,最简单的解决方案是使用 GUI 会话中的 Generate transfer code 函数,以获得工作代码模板。
- 登录GUI;
- Select远程面板要下载的文件,本地面板要下载的目标本地目录;
- 使用下载命令;
- 在 Download transfer options dialog 上,使用 传输设置 > 生成代码;
Generate transfer code dialog 打开。 Select .NET 程序集选项卡 并确保选择 C# 语言。