将文件上载到c#net中的FTPS
有谁能提供一个简短的指南,说明如何使用C#中的自签名证书将文件发送到FTPS服务器?我通过SFTP和FTP处理文件传输的速度非常快,但我会放弃FTP。我已经用FtpWebRequest和fluentFtp尝试了几天,我不确定我的代码是错误的还是我在我的机器上配置FTPS的方式,所以我真的很感激这两种方法。下面我提供了我在例外情况下测试过的代码将文件上载到c#net中的FTPS,c#,.net,iis,ftps,self-signed-certificate,C#,.net,Iis,Ftps,Self Signed Certificate,有谁能提供一个简短的指南,说明如何使用C#中的自签名证书将文件发送到FTPS服务器?我通过SFTP和FTP处理文件传输的速度非常快,但我会放弃FTP。我已经用FtpWebRequest和fluentFtp尝试了几天,我不确定我的代码是错误的还是我在我的机器上配置FTPS的方式,所以我真的很感激这两种方法。下面我提供了我在例外情况下测试过的代码 public bool Send(string filePath)//webRequest version { bool
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我使用的是FluentFTP和以下代码,以及由证书颁发机构签署的证书:
private FtpClient getFtpsClient(System.Uri uri) {
if (uri.Scheme != "ftps") {
throw new NotImplementedException("Only ftps is implementent");
}
var userInfo = uri.UserInfo.Split(":");
FtpClient client = new FtpClient(uri.Host, userInfo[0], userInfo[1]);
client.EncryptionMode = FtpEncryptionMode.Explicit;
client.SslProtocols = SslProtocols.Tls;
client.ValidateCertificate += new FtpSslValidation(OnValidateCertificate);
client.Connect();
void OnValidateCertificate(FtpClient control, FtpSslValidationEventArgs e) {
var cert2 = new X509Certificate2(e.Certificate);
e.Accept = cert2.Verify();
}
return client;
}
private void writeFileFtps(System.Uri uri, string fileName, byte[] content)
{
var ftpsClient = getFtpsClient(uri);
ftpsClient.Upload(content, uri.LocalPath + "/" + fileName);
ftpsClient.Disconnect();
}
到目前为止还可以,但是
在FluentFTP的常见问题解答中,您可以找到有关使用的信息。当我将ftPencryOptionMode更改为隐式时,它就开始工作了。你知道这是为什么吗?编辑:当我将FTPS服务器端口更改为990以外的端口时,我可以使用显式模式,但我对正在发生的事情的了解更少。你看到问题了吗?