I can connect to SFTP using the FileZilla in a machine, in the same machine when I try to connect via C# code it gives error as
Network error: Connection to "fs2.comparethemarket.com" timed out
SFTP port used for connection;2222
Code I am using
SessionOptions sessionOptions = new SessionOptions();
if (_strftpprotocol=="SFTP")
{
sessionOptions.Protocol = WinSCP.Protocol.Sftp;
}
else if(_strftpprotocol == "FTP")
{
sessionOptions.Protocol = WinSCP.Protocol.Ftp;
sessionOptions.FtpSecure = FtpSecure.Explicit;
}
sessionOptions.HostName = _strftpURL;
sessionOptions.UserName = _strftpUserName;
sessionOptions.Password = _strftpPassword;
if (_strftpprotocol == "SFTP")
{
if (string.IsNullOrEmpty(_strftpSSHKey))
{
if (string.IsNullOrEmpty(_strftpKeyPath))
{
sessionOptions.GiveUpSecurityAndAcceptAnySshHostKey = true;
}
else
{
privatekeyFilePath = privatekeyFilePath + _strftpKeyPath;
sessionOptions.SshPrivateKeyPath = privatekeyFilePath;
if (!string.IsNullOrEmpty(_strFTPPrivateKeyPassphrase))
{
sessionOptions.PrivateKeyPassphrase = _strFTPPrivateKeyPassphrase;
}
if (!string.IsNullOrEmpty(_strFTPSshHostKeyFingerprint))
{
sessionOptions.SshHostKeyFingerprint = _strFTPSshHostKeyFingerprint;
}
}
}
else
{
if (string.IsNullOrEmpty(_strftpKeyPath))
{
sessionOptions.SshHostKeyFingerprint = _strftpSSHKey;
}
else
{
privatekeyFilePath = privatekeyFilePath + _strftpKeyPath;
sessionOptions.SshPrivateKeyPath = privatekeyFilePath;
if (!string.IsNullOrEmpty(_strFTPPrivateKeyPassphrase))
{
sessionOptions.PrivateKeyPassphrase = _strFTPPrivateKeyPassphrase;
}
if (!string.IsNullOrEmpty(_strFTPSshHostKeyFingerprint))
{
sessionOptions.SshHostKeyFingerprint = _strFTPSshHostKeyFingerprint;
}
}
}
}
Session session = new Session();
session.Open(sessionOptions);
Please guide me what I am doing wrong
Per the WinSCP Docs,
Specifying the
Dst Port
the SFTP Client tries to connect to is done through setting the sessionOptions
Port Number
property.
It's necessary only if the server listens on a non-default port (22), which in your case, it is. (2222).
So to solve your timeout issue,
set the Dst Port
with SessionOptions.PortNumber=2222
(or better yet, save it in a conf file or wherever those other variables (Hostname, username,password) are being set)