Could anyone provide short guide how to send files to FTPS server with self signed certificate in C#? I coped with file transfer through SFTP and FTP really quick, but I'm gonna loose my mind on FTPS. I've tried for few days with FtpWebRequest and fluentFtp and I am not sure if my code is bad or the way I configured FTPS on my machine, so I would really apprieciate guide through both. Below I provide code wchich I have tested with exceptions.
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;
}
For my purpose I can use only free nugets