简体   繁体   中英

Uploading files to FTP Server

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..

只需在应用的入口点添加此行:

System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);

You are probably hitting the default connection limit for ServicePoint.ConnectionLimit Property , which is 2 . The FtpWebRequest has a ServicePoint property that you can adjust. You will need to close your uploadStream as soon as the upload is finished.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM