我有一个要求就是需要创建一个C#应用程序,它会将一个excel文件上传到" FTP / SFTP"服务器基于app.config
文件中输入的设置(使用" ftp \ ftps \ 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);
答案 0 :(得分:6)
如果您的代码能够处理FTPS,它通常也能够处理FTP,但是有很多代码只能处理FTP而不能处理FTPS。由于SFTP是一个完全不同的协议代码处理FTP / FTPS通常无法做SFTP。而SFTP处理代码不会做FTP / FTPS。有一些例外,即FileZilla可以在单个应用程序中处理所有这些协议。
至于将FTPS与FtpWebRequests一起使用,请参阅msdn。无法使用SFTP和FtpWebRequests,但有other libraries。
答案 1 :(得分:5)
SFTP和FTP / FTPS是两种完全不同的协议。您无法使用FTP上传到SFTP服务器,反之亦然。 FTPS是FTP over TLS / SSL会话。大多数FTP客户端/库也支持FTPS。
.NET框架本身仅支持FTP(S)(通过FtpWebRequest
class)。要使用FTPS,请使用ftps://
网址或将FtpWebRequest.EnableSsl
设置为true
。
.NET框架中没有SFTP的本机支持。您必须使用3rd party library for the SFTP。
有些库为所有这些协议提供统一的接口。
例如,使用WinSCP .NET assembly,(几乎)仅将SessionOptions.Protocol
设置为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);
FTP协议:
SessionOptions sessionOptions = new SessionOptions {
Protocol = Protocol.Ftp,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
};
Session session = new Session();
session.Open(sessionOptions);
FTPS协议:
SessionOptions sessionOptions = new SessionOptions {
Protocol = Protocol.Ftp,
FtpSecure = FtpSecure.Explicit,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
};
Session session = new Session();
session.Open(sessionOptions);
如果您需要使会话可配置,可以使用SessionOptions.ParseUrl
轻松实现,使用您在配置文件中设置的单个连接字符串(URL)来配置主要会话选项
SessionOptions sessionOptions = new SessionOptions();
sessionOptions.ParseUrl(connectionString);
Session session = new Session();
session.Open(sessionOptions);
连接字符串可以是:
sftp://user@mypassword;fingerprint=ssh-rsa-xxxxxxxxxxx...=@example.com
ftp://user@mypassword@example.com
ftpes://user@mypassword@example.com
您可以拥有WinSCP (GUI) generate the URL (connection string) for you。
请注意,WinSCP .NET程序集不是本机.NET库。它只是一个围绕控制台应用程序(WinSCP)的瘦.NET包装。
可能存在本机.NET库,它们支持具有统一接口的所有协议。但我不知道有任何免费的。
(我是WinSCP的作者)
答案 2 :(得分:1)
将鼠标悬停在问题下的标记上可以轻松找到。 FTP和SFTP是完全不同的协议。 FTPS是带加密的FTP
没有
如果您使用的是WebRequestMethods.Ftp.UploadFile;
,它将无法用于SFTP,也可能用于FTPS,但必须有一些选项可以启用加密。
答案 3 :(得分:0)
对于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"
};
如果您需要下载帮助,上传获取文件列表请与我联系