Frage

In my Web application I need to set path to two FTP folders and in model validation I try to check if these folders exists on the FTP server. My problem is that first time this usually works and the second time usually too. But the third time I have this error 'The remote server returned an error: (530) Not logged in.'

Here is my code:

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);
 } 

After some time everything becomes ok but after 2-3 validations (run this code) the "not logged in issue" comes back. What have I missed?

Moreover if I uncomment response.Close(); and if files are present in a folder (there are about 25 mb of files inside) it can hang at all. What's wrong here?

**Edit1: ** If I use PrintWorkDirectory instead ListDirectory it does not throw an exception whatever directory I check. At the picture is a response object of not existing directory on server

enter image description here

And here is response object of existing folder. It's totally the same... enter image description here

War es hilfreich?

Lösung 2

Here is a solution what helped for me

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);
   }
}

And method

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;
} 

I'm not sure why it does not work before and if somebody shows this it will be very appreciated :)

Now this code works without login issues and also do not hang. Maybe it works now because we read resonce what we did not do before and close responce (without reading it hanged in past)

Andere Tipps

First thing first, ListDirectory will list all the files in the folder which can be problematic when there are tons of files in the FTP folder. Try using:

PrintWorkingDirectory (alternatively GetDateTimestamp ). For the 'Not logged' error, try setting UsePassive to false. This has solved many 'Not logged' errors for me.

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);
}

...
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top