.Net FtpWebRequest fails sometimes

天大地大妈咪最大 提交于 2020-01-03 08:52:49

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/1559232/net-ftpwebrequest-fails-sometimes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!