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
And here is response object of existing folder. It's totally the same...