int WriteToFtp(Stream src, IActivityIOPath dst)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ConvertSslToPlain(dst.Path));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
request.KeepAlive = false;
request.EnableSsl = EnableSsl(dst);
if(dst.Username != string.Empty)
{
request.Credentials = new NetworkCredential(dst.Username, dst.Password);
}
if(dst.IsNotCertVerifiable)
{
ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertifications;
}
request.ContentLength = src.Length;
using(Stream requestStream = request.GetRequestStream())
{
using(src)
{
byte[] payload = src.ToByteArray();
int writeLen = payload.Length;
requestStream.Write(payload, 0, writeLen);
}
}
var result = (int)request.ContentLength;
using(var response = (FtpWebResponse)request.GetResponse())
{
if(response.StatusCode != FtpStatusCode.FileActionOK && response.StatusCode != FtpStatusCode.ClosingData)
{
throw new Exception("File was not created");
}
}
return result;
}