0

我正在尝试获取服务器上的文件列表

代码:

string ftpUserID = "user";
string ftpPassword = "password";
string ftpServerIP = "192.###.###.###";
string remoteDirectory = @"\Update\UpdateTest";
string localDirectory = @"C:\Updates";

string[] downloadFiles;
StringBuilder result = new StringBuilder();
WebResponse response = null;
StreamReader reader = null;

FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" +      remoteDirectory));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
reqFTP.EnableSsl = true;
reqFTP.Proxy = null;
reqFTP.KeepAlive = true;
reqFTP.UsePassive = true;
response = reqFTP.GetResponse();
reader = new StreamReader(response.GetResponseStream());

string line = reader.ReadLine();
while (line != null)
{
    result.Append(line);
    result.Append("\n");
    line = reader.ReadLine();
}
result.Remove(result.ToString().LastIndexOf('\n'), 1);
return result.ToString().Split('\n');

我不断收到 WebException 错误提示“无法连接到远程服务器”

这是由于以下错误造成的:

 FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" +      remoteDirectory));

我认为这是在reqFTP.ContentreqFTP.PreAuthenticate部分引发System.NotSuportedException 。

4

1 回答 1

0

您提到,尽管它在代码中不可见,但您使用PreAuthenticate. FTP 不支持此功能。

根据msdn ,仅提供该PreAuthenticate属性是为了与 WebRequest 和 WebResponse 类的其他实现兼容。

于 2013-12-05T13:01:10.703 回答