FtpWebRequest问题~错误信息为“请求的URI对此FTP命令无效”
各位好~遇上了个问题。。。在一个方法中建立了FtpWebRequest对象,已经指向FTP的根目录了,我读取完我需要的数据(读取所有的文件名)后,想要跳到根目录下的每个目录,再次读取每个目录下的文件名,这时,报的错误信息是“请求的URI对此FTP命令无效”,请问是什么原因?谢谢了~~我在猜测是不是由于每次建立的FTP连接只能是一个?
public static string[] FTPGetFileList(string ftpServerIP, string ftpUserID, string ftpPassword) { //响应结果 StringBuilder result = new StringBuilder(); //FTP请求 FtpWebRequest ftpRequest = null; //FTP响应 WebResponse ftpResponse = null; //FTP响应流 StreamReader ftpResponsStream = null; try { //生成FTP请求 ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/")); //设置文件传输类型 ftpRequest.UseBinary = true; //FTP登录 ftpRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword); //设置FTP方法 ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; //生成FTP响应 ftpResponse = ftpRequest.GetResponse(); //FTP响应流 ftpResponsStream = new StreamReader(ftpResponse.GetResponseStream(), System.Text.Encoding.Default); Console.WriteLine(ftpResponsStream.CurrentEncoding); string line = ftpResponsStream.ReadLine(); while (line != null) { if (line.IndexOf("<DIR>") != -1) { Console.WriteLine(line.Substring(39));//此处获得目录名,测试用 result.Append(GetSubFilesList(ftpServerIP,ftpUserID,ftpPassword,line.Substring(39))); } else { result.Append(line); result.Append("\n"); } line = ftpResponsStream.ReadLine(); } //去掉结果列表中最后一个换行 result.Remove(result.ToString().LastIndexOf('\n'), 1); //返回结果 return result.ToString().Split('\n'); } catch (Exception ex) { Console.WriteLine(ex.Message); return (null); } finally { if (ftpResponsStream != null) { ftpResponsStream.Close(); } if (ftpResponse != null) { ftpResponse.Close(); } } }private static string GetSubFilesList(string ftpServerIP,string ftpUserID, string ftpPassword, string StrDir) { //响应结果 StringBuilder result = new StringBuilder(); string struri = "ftp://" + ftpServerIP + "/" + StrDir + "/"; //FTP请求 FtpWebRequest ftpRequest = null; //FTP响应 WebResponse ftpResponse = null; //FTP响应流 StreamReader ftpResponsStream = null; try { ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(struri)); //生成FTP响应 ftpResponse = ftpRequest.GetResponse(); //FTP响应流 ftpResponsStream = new StreamReader(ftpResponse.GetResponseStream(), System.Text.Encoding.Default); Console.WriteLine(ftpResponsStream.CurrentEncoding); string line = ftpResponsStream.ReadLine(); while (line != null) { if (line.IndexOf("<DIR>") != -1) { Console.WriteLine(line.Substring(39)); result.Append(GetSubFilesList(ftpServerIP,ftpUserID,ftpPassword,line.Substring(39)));//此处希望通过递归调用获取子目录下的文件名 } else { result.Append(line); result.Append("\n"); } line = ftpResponsStream.ReadLine(); } return result.ToString(); } ... }