title: Uploading files to FTP Server
body: <p>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</p>
  
  <pre><code>Stream uploadStream = reqFTP.GetRequestStream();
  </code></pre>
  
  <p>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.</p>
  
  <p>here is the full code:</p>
  
  <pre><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);
              }
  
          }
  </code></pre>
  
  <p>As i saied, when i reach the uloadStream code it checks my certificats, here is my certificate method</p>
  
  <pre><code> public bool Certificate(Object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors)
          {
              { return cert.Issuer == "myCertificate"; }
          }
  </code></pre>
  
  <p>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..</p>