Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net FtpWebRequest fails sometimes

I try to list file details using FtpWebRequest but very frequently it fails with a WebException and shows error 530 User not logged in.

How is this possible, that it works some of the time using the same credentials?

Excerpt from code:

        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpuri));
        reqFTP.UseBinary = true;
        reqFTP.Credentials = new NetworkCredential(userName, password);
        string[] downloadFiles = new string[0];
            reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            WebResponse response = reqFTP.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            downloadFiles = reader.ReadToEnd().Replace("\r\n", "¤").Split('¤');
            reader.Close();
            response.Close();
like image 475
Henrik Avatar asked Oct 13 '09 09:10

Henrik


1 Answers

Try setting

reqFTP.KeepAlive = false;

and possibly if the above does not work

reqFTP.UsePassive = false;

I found that setting these to false reduced the occurrences of this error (which is generated by the FTP server) considerably.

like image 159
Peter Avatar answered Oct 14 '22 11:10

Peter