1

对于某些由于某种原因无法正常工作的代码,我需要一些帮助。我正在制作一种获取 FTP 目录中文件列表的方法。每次我调试应用程序时,都会抛出一个 WebException,StatusCode 为 530(未登录)。 请记住,我 100% 肯定地址、用户名和密码是正确的。 这是方法:

public static List<string> GetFileList(string Directory)
    {
        List<string> Files = new List<string>();
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ServerInfo.Root + Directory));
        request.Method = WebRequestMethods.Ftp.ListDirectory;
        request.Credentials = new NetworkCredential(ServerInfo.Username, ServerInfo.Username);
        FtpWebResponse response = (FtpWebResponse)request.GetResponse(); //Error occurs here
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        string CurrentLine = reader.ReadLine();
        while (!string.IsNullOrEmpty(CurrentLine))
        {
            Files.Add(CurrentLine);
            CurrentLine = reader.ReadLine();
        }
        reader.Close();
        response.Close();
        return Files;
    }

这是 ServerInfo.Root 的值:“ ftp://192.xxx.4.xx:21/MPDS ”(为了隐私而部分审查)

我使用 MessageBoxes 来确保完整的 URI 是正确的,而且确实如此。

我已经为这个问题苦苦挣扎了很长时间,所以我希望你能帮助我解决它。

提前致谢!

4

1 回答 1

2

您可以尝试此代码并进行一些更正:

public static List<string> GetFileList(string Directory)
    {
        List<string> Files = new List<string>();

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ServerInfo.Root + Directory));
        request.Method = WebRequestMethods.Ftp.ListDirectory;

        request.Credentials = new NetworkCredential(ServerInfo.Username, ServerInfo.Username); // Is this correct?
        // request.Credentials = new NetworkCredential(ServerInfo.Username, ServerInfo.Password); // Or may be this one?

        request.UseBinary = false;
        request.UsePassive = true;

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

        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        string CurrentLine = reader.ReadLine();
        while (!string.IsNullOrEmpty(CurrentLine))
        {
            Files.Add(CurrentLine);
            CurrentLine = reader.ReadLine();
        }
        reader.Close();
        response.Close();
        return Files;
    }
于 2014-08-05T02:19:28.630 回答