[英]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. 我试图ulploade文件和ftp服务器,但当我运行该方法时,它只上传2个文件,然后停止。 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 正如我所说,当我到达uloadStream代码时它会检查我的证书,这是我的证书方法
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?? 有没有办法只连接到ftp服务器一次,并执行一次证书并保持会话? 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
. 您可能正在达到ServicePoint.ConnectionLimit属性的默认连接限制,即
2
。 The FtpWebRequest
has a ServicePoint
property that you can adjust. FtpWebRequest
具有可以调整的ServicePoint
属性。 You will need to close your uploadStream
as soon as the upload is finished. 上传完成后,您需要关闭
uploadStream
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.