FtpWebRequest:间歇性500语法错误;如何追踪? [英] FtpWebRequest: intermittent 500 Syntax error; how to trace?

查看:124
本文介绍了FtpWebRequest:间歇性500语法错误;如何追踪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 -
在进行大批量的FTP操作(200-400)时,我从服务器返回间歇性语法错误(通过异常报告)。重新运行相同的序列时,它在相同的操作或文件上永远不会失败。使用较小的批次它有时只会失败....但现在它一直失败。


解决方案

仅供参考,这是通常失败的代码。导致下载的几个步骤(列表目录,抓取文件属性)有时会失败,但由于它与请求的数量有关,因此最后一个请求循环通常是混乱的。

这是从其他人的代码重新编写的。 FtpFile一个Ftp文件信息的结构; llf是List< FtpFile>要下载的文件。

foreach(fffpFile ff in lff){

FileInfo fi = new FileInfo( Path.Combine(batchDir.FullName,ff.Name));

req =(FtpWebRequest)FtpWebRequest.Create(ftp_addr +'/'+ ff.Name);
req.Credentials = new NetworkCredential(ftp_user,ftp_pwd);
req.Method = WebRequestMethods.Ftp.DownloadFile;
req.UseBinary = true; //谨慎但不必为此
req.KeepAlive = true; //没影响
req.UsePassive = true; //没影响

FtpWebResponse resp =(FtpWebResponse) req.GetResponse();
Stream fis = resp.GetResponseStream();
FileStream fos = fi.Create();

int bufferSize = 2048;
byte [] buffer = new byte [bufferSize];
int readCount = fis.Read(buffer,0,bufferSize);
while(readCount> 0){
fos.Write(buffer,0,readCount);
readCount = fis.Read(buffer,0,bufferSize);
}

fos.Close();
fis.Close();
resp.Close();

lfi.Add(fi); //添加到本地存储文件列表


Hi --
I'm getting intermittent syntax errors back from the server (reported via the exception) when doing large batches of FTP operations  (200-400).   It never fails on the same operation or file when rerunning the identical sequence.  Using smaller batches it only sometimes failed.... but now it fails consistently.


解决方案

FYI, this is the code that's usually failing.   There are several steps leading up to the download (list directories, grab file attributes)  that sometimes fail, but since it's related to the number of requests, this last loop of requests is typically the one that messes up.

This is reworked from someone else's code.   FtpFile a struct of Ftp file info;  llf is a List<FtpFile> of files to be downloaded.

        foreach (FtpFile ff in lff)  {
       
            FileInfo fi = new FileInfo(Path.Combine(batchDir.FullName, ff.Name));

            req = (FtpWebRequest)FtpWebRequest.Create(ftp_addr + '/' + ff.Name);
            req.Credentials = new NetworkCredential (ftp_user, ftp_pwd);
            req.Method = WebRequestMethods.Ftp.DownloadFile;
            req.UseBinary = true;     //cautious but not necessary for this
            req.KeepAlive = true;     //no effect
            req.UsePassive = true;    //no effect

            FtpWebResponse resp = (FtpWebResponse)req.GetResponse();   
            Stream fis = resp.GetResponseStream();
            FileStream fos = fi.Create();

            int bufferSize = 2048;
            byte[] buffer = new byte[bufferSize];
            int readCount = fis.Read(buffer, 0, bufferSize);
            while (readCount > 0)   {
                fos.Write(buffer, 0, readCount);
                readCount = fis.Read(buffer, 0, bufferSize);
            }
           
            fos.Close();
            fis.Close();
            resp.Close();
         
            lfi.Add(fi);  //add to List of locally stored files


这篇关于FtpWebRequest:间歇性500语法错误;如何追踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆