谁能提供简短指南如何使用 C# 中的自签名证书将文件发送到 FTPS 服务器?我很快就通过 SFTP 和 FTP 处理了文件传输,但我会对 FTPS 失去理智。我已经用 FtpWebRequest 和 fluentFtp 尝试了几天,但我不确定我的代码是否错误或者我在机器上配置 FTPS 的方式,所以我真的很想通过这两个指南。下面我提供了我测试过的异常代码。
public bool Send(string filePath)//webRequest version
{
bool isFileUploaded = false;
try
{
FtpWebRequest request = WebRequest.Create($"ftp://{this.DestinationInfo.HostIp}:{this.DestinationInfo.Port}/ftps_test/test.txt") as FtpWebRequest;
request.Credentials = this.Credentials; //hostIp is the same machine Ip // port is 990
request.EnableSsl = true;
request.Method = WebRequestMethods.Ftp.UploadFile;
using (Stream fileStream = File.OpenRead(filePath))
using (Stream ftpStream = request.GetRequestStream())
{
fileStream.CopyTo(ftpStream);
}
isFileUploaded = true;
}
catch (Exception exception)
{
//logging -> exception.Message is "System error" and nothing else
isFileUploaded = false;
}
return isFileUploaded;
}
public bool Send(string filePath)// fluentFtp version
{
bool isFileUploaded = false;
try
{
FtpClient client = new FtpClient(this.DestinationInfo.HostIp, this.DestinationInfo.UserName, this.DestinationInfo.Password);
//also tried "ftp://{HostIp}"
client.Port = this.DestinationInfo.Port;//990
client.ReadTimeout *= 10; //temporary because of timeout
client.EncryptionMode = FtpEncryptionMode.Implicit;
client.SslProtocols = SslProtocols.Default;
client.Connect();
client.UploadFile(filePath, "test.csv");
isFileUploaded = true;
}
catch (Exception exception)
{
//logging -> exception.Message is "According to the validation procedure, the remote certificate is invalid.", but i have not idea why
isFileUploaded = false;
}
return isFileUploaded;
}
出于我的目的,我只能使用免费的 nugets