Im trying to ulploade files to and ftp Server, but when i run the method it uploades only 2 files and then stalls. It stalls on this line
Stream uploadStream = reqFTP.GetRequestStream();
When i reach this line the first 2 times, the program checks my certificate and then continues, but the third time it stalls and never goes on checking my certificates.
here is the full code:
public void UploadLocalFiles(string folderName)
{
try
{
string localPath = @"\\localFolder\" + folderName;
string[] files = Directory.GetFiles(localPath);
string path;
foreach (string filepath in files)
{
string fileName = Path.GetFileName(filepath);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://serverIP/inbox/"+fileName));
reqFTP.UsePassive = true;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential("username", "password");
reqFTP.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback = Certificate;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
FileInfo fileInfo = new FileInfo(localPath +@"\"+ fileName);
FileStream fileStream = fileInfo.OpenRead();
int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];
Stream uploadStream = reqFTP.GetRequestStream();
int contentLength = fileStream.Read(buffer, 0, bufferLength);
while (contentLength != 0)
{
uploadStream.Write(buffer, 0, bufferLength);
contentLength = fileStream.Read(buffer, 0, bufferLength);
}
}
}
catch (Exception e)
{
Console.WriteLine("Error in GetLocalFileList method!!!!!" + e.Message);
}
}
As i saied, when i reach the uloadStream code it checks my certificats, here is my certificate method
public bool Certificate(Object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors)
{
{ return cert.Issuer == "myCertificate"; }
}
Is there some way to only connect to ftp server once, and do the certificate once and hold the session?? cuz right each time i want to uploade or download a file i connect and verify the certificate for each file..
You are probably hitting the default connection limit for ServicePoint.ConnectionLimit Property, which is
2. TheFtpWebRequesthas aServicePointproperty that you can adjust. You will need to close youruploadStreamas soon as the upload is finished.