C#WebException错误:530(未登录)

时间:2014-08-05 01:11:57

标签: c# ftp webexception

我需要帮助一些因某些原因无效的代码。我正在制作一个获取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的值:&#34; ftp://192.xxx.4.xx:21/MPDS&#34; (部分审查隐私)

我使用了MessageBoxes来确保完整的URI是正确的,而且它是。

我很长一段时间以来一直在努力解决这个问题,所以我希望你能帮我解决这个问题。

提前致谢!

1 个答案:

答案 0 :(得分: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;
    }