经过一些连接,我有一个“未登录”的问题

时间:2013-04-11 20:08:27

标签: c# login ftp ftpwebrequest

在我的Web应用程序中,我需要设置两个FTP文件夹的路径,在模型验证中,我尝试检查FTP服务器上是否存在这些文件夹。 我的问题是第一次这通常有效,第二次通常也是如此。但第三次出现此错误'远程服务器返回错误:(530)未登录。'

这是我的代码:

try
 {
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(model.configData.FTPInputFolder));
    request.Credentials = new NetworkCredential("ftp_free", "ftp_free");
    request.Method = WebRequestMethods.Ftp.ListDirectory;
    request.KeepAlive = false;

    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    /*response.Close();*/        
 }
 catch (WebException e)
 {
    ModelState.AddModelError("configData.FTPInputFolder", "Directory does not exist " + e.Message);
 }
 // second request for one other directory
 try
 {
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(model.configData.FTPOutputFolder));
    request.Credentials = new NetworkCredential("ftp_free", "ftp_free");
    request.Method = WebRequestMethods.Ftp.ListDirectory;
    request.KeepAlive = false;

    FtpWebResponse response = (FtpWebResponse)request.GetResponse();

 }
 catch (WebException e)
 {
    ModelState.AddModelError("configData.FTPOutputFolder", "Directory does not exist" + e.Message);
 } 

一段时间后一切都变好了,但经过2-3次验证(运行此代码)后,“未登录的问题”又回来了。我错过了什么?

此外,如果我取消注释response.Close();并且文件夹中存在文件(内部大约有25 MB的文件),它可以完全挂起。这有什么不对?

** Edit1:**如果我使用PrintWorkDirectory代替ListDirectory,它不会在我检查的任何目录中抛出异常。图中是服务器上不存在目录的响应对象

enter image description here

这是现有文件夹的响应对象。它完全一样...... enter image description here

2 个答案:

答案 0 :(得分:0)

首先,ListDirectory将列出文件夹中的所有文件,当FTP文件夹中有大量文件时,这些文件可能会出现问题。尝试使用:

PrintWorkingDirectory(或者GetDateTimestamp)。对于“未记录”错误,请尝试将UsePassive设置为false。这为我解决了许多“未记录”的错误。

try
{
   FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(model.configData.FTPInputFolder));
   request.Credentials = new NetworkCredential("ftp_free", "ftp_free");
   request.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;
   request.KeepAlive = false;
   request.UsePassive = false;

   using(FtpWebResponse response = (FtpWebResponse)request.GetResponse())
   {}
}
catch (WebException e)
{
  ModelState.AddModelError("configData.FTPInputFolder", "Directory does not exist " + e.Message);
}

...

答案 1 :(得分:0)

这是一个对我有帮助的解决方案

try
{
   FTPOperations.ListDirectory(model.configData.FTPInputFolder, model.configData.FTPLogin, model.configData.FTPPassword);
}
catch (WebException e)
{
   FtpWebResponse response = (FtpWebResponse)e.Response;
   if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
   {
      ModelState.AddModelError("configData.FTPInputFolder", "Directory does not exists");
   }
   else
   {
      ModelState.AddModelError("configData.FTPInputFolder", "Directory check error: " + e.Message);
   }
}

方法

public static List<string> ListDirectory(string dirPath, string ftpUser, string ftpPassword)
{
   List<string> res = new List<string>();

   FtpWebRequest request = (FtpWebRequest)WebRequest.Create(dirPath);
   request.Method = WebRequestMethods.Ftp.ListDirectory;
   request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
   request.KeepAlive = false;

   FtpWebResponse response = (FtpWebResponse)request.GetResponse();

   Stream responseStream = response.GetResponseStream();
   StreamReader reader = new StreamReader(responseStream);
   while (!reader.EndOfStream)
   {
      res.Add(reader.ReadLine());
   }
   reader.Close();
   response.Close();

   return res;
} 

我不确定为什么它之前没有用,如果有人证明这一点会非常感激:)

现在此代码无需登录问题,也不会挂起。也许它现在有效,因为我们阅读了我们之前没有做过的事情,并且关闭了回应(没有阅读它在过去被绞死)