C# FTP/FTPS/SFTP之间的差异-可配置到其中任何一个的连接
我有一个要求,比如需要创建一个C#应用程序,根据C# FTP/FTPS/SFTP之间的差异-可配置到其中任何一个的连接,c#,ftp,sftp,ftps,C#,Ftp,Sftp,Ftps,我有一个要求,比如需要创建一个C#应用程序,根据app.config文件中输入的设置(使用“FTP\ftps\SFTP”)将excel文件上传到“FTP/SFTP”服务器 我对这些协议很陌生,有很多疑问 FTP和SFTP服务器之间的区别是什么 是否可以使用SFTP连接方法访问FTP服务器,反之亦然(使用Rebex库连接到SFTP的指南) 如何将以下FTP上载方法更改为FTPS 代码如下: string PureFileName = new FileInfo(fileName).Name; str
app.config
文件中输入的设置(使用“FTP\ftps\SFTP”)将excel文件上传到“FTP/SFTP”服务器
我对这些协议很陌生,有很多疑问
string PureFileName = new FileInfo(fileName).Name;
string uploadUrl = String.Format("ftp://{0}/{1}", ftpurl, PureFileName);
FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(uploadUrl);
req.Proxy = null;
req.Method = WebRequestMethods.Ftp.UploadFile;
req.Credentials = new NetworkCredential(user, pass);
req.UseBinary = true;
req.UsePassive = true;
byte[] data = File.ReadAllBytes(fileName);
req.ContentLength = data.Length;
Stream stream = req.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
FtpWebResponse res = (FtpWebResponse)req.GetResponse();
这就像将url
从FTP更改为FTPS一样吗
string uploadUrl = String.Format("ftps://{0}/{1}", ftpurl, PureFileName);
WebRequestMethods.Ftp.UploadFile代码>它不适用于SFTP,可能适用于FTPS,但必须有一些选项来打开加密
- FTP:旧的文件传输协议(RFC959)。防火墙有问题,因为它使用动态端口,并且有关这些端口的信息在应用程序级别交换
- FTPS:旧的FTP协议,但增加了对TLS的支持。防火墙的问题更大,因为它们无法再查看应用程序级别以找出使用了哪些端口
- SFTP:完全不同,因为它使用SSH协议传输文件。防火墙没有问题
true
在.NET framework中没有对SFTP的本机支持。你必须使用一个
有一些库为所有这些协议提供统一的接口 例如,对于,它(几乎)只是关于将设置为
Protocol.FTP
或Protocol.SFTP
SFTP协议:
SessionOptions sessionOptions = new SessionOptions {
Protocol = Protocol.Sftp,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...="
};
Session session = new Session();
session.Open(sessionOptions);
SessionOptions sessionOptions = new SessionOptions {
Protocol = Protocol.Ftp,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
};
Session session = new Session();
session.Open(sessionOptions);
SessionOptions sessionOptions = new SessionOptions {
Protocol = Protocol.Ftp,
FtpSecure = FtpSecure.Explicit,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
};
Session session = new Session();
session.Open(sessionOptions);
FTP协议:
SessionOptions sessionOptions = new SessionOptions {
Protocol = Protocol.Sftp,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...="
};
Session session = new Session();
session.Open(sessionOptions);
SessionOptions sessionOptions = new SessionOptions {
Protocol = Protocol.Ftp,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
};
Session session = new Session();
session.Open(sessionOptions);
SessionOptions sessionOptions = new SessionOptions {
Protocol = Protocol.Ftp,
FtpSecure = FtpSecure.Explicit,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
};
Session session = new Session();
session.Open(sessionOptions);
FTPS协议:
SessionOptions sessionOptions = new SessionOptions {
Protocol = Protocol.Sftp,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...="
};
Session session = new Session();
session.Open(sessionOptions);
SessionOptions sessionOptions = new SessionOptions {
Protocol = Protocol.Ftp,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
};
Session session = new Session();
session.Open(sessionOptions);
SessionOptions sessionOptions = new SessionOptions {
Protocol = Protocol.Ftp,
FtpSecure = FtpSecure.Explicit,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
};
Session session = new Session();
session.Open(sessionOptions);
如果需要对会话进行配置,可以通过使用使您可以使用在配置文件中设置的单个连接字符串(URL)来配置主要会话选项,从而简化会话的配置
SessionOptions sessionOptions = new SessionOptions();
sessionOptions.ParseUrl(connectionString);
Session session = new Session();
session.Open(sessionOptions);
连接字符串可以类似于:
- SFTP:
sftp://user@我的密码;指纹=ssh-rsa-xxxxxxxxxx…=@example.com
- FTP:
ftp://user@mypassword@example.com
- FTPS:
ftpes://user@mypassword@example.com
请注意,WinSCP.NET程序集不是本机.NET库。它只是一个围绕控制台应用程序()的薄型.NET包装器 可能有一些本机.NET库支持具有统一接口的所有协议。但我不知道有免费的 (我是WinSCP的作者)对于FTPS
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "address.co.za",
FtpSecure = FtpSecure.Explicit,
UserName = "username",
Password = "pass",
TlsHostCertificateFingerprint = "xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
};
如果您需要下载、上传获取文件列表的帮助,请与我联系@您能给我一些实现ftps上传的代码示例吗如果您想了解SFTP和FTP/ftps的区别,欢迎阅读我在CodeGuru上的文章: