我正在尝试使用SSH.NET库访问FTP服务器,但没有任何运气。我提供了与FileZilla相同的证书,运行良好。SSH抛出错误“套接字读取操作已超时”。如果我使用与下面相同的代码,但没有指定端口:21,我得到一个错误:“用户无法进行身份验证”。有人能提供一些见解吗?
string tempHost = @"ftp.mywebsite.com";
string tempUser = @"ftp@mywebsite.com";
string tempPassword = @"try123";
using (SftpClient sftpClient =
new SftpClient((ConnectionInfo)new PasswordConnectionInfo(tempHost,21, tempUser, tempPassword)))
{
sftpClient.Connect();
}发布于 2014-05-06 07:39:34
如果你仍然不能解决这个问题,下面的代码对我很有效
using (var sftp = new SftpClient(host, userName, password))
{
sftp.Connect();
//Do some operation
sftp.Disconnect();
}Ananth
发布于 2019-10-03 08:40:59
SSH工具(如Renci SSH)应在端口22 (安全)上使用。如果要连接到端口21,则需要另一个库。我使用了FluentFTP,它不像Renci那么容易使用,但可以完成工作。
以下是可用于将文件上传到服务器(在版本19.1.2上)的代码示例。无论您使用的是端口21还是22,我都强烈建议写操作之间至少间隔500ms,以便让服务器有时间进行呼吸。
using (FtpClient client = new FtpClient())
{
client.Host = ftpAddress;
client.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
client.Port = 21;
client.Connect();
using (Stream s = new FileStream(localPathWithFileName, FileMode.Open))
{
try
{
//log.Info($"Uploading file...");
client.Upload(s, ftpFilePathWithFileName);
//log.Info($"File uploaded!");
}
catch(Exception e)
{
//log.Info($"{e.StackTrace}");
}
finally
{
client.Disconnect();
}
}
}https://stackoverflow.com/questions/21626328
复制相似问题