3

I am struggling with a small piece of code which I am currently writing. The application is supposed to run once a day and download all files from an ftp server. My problem is:

Although in theory my routine to list the direcoty content runs fine, checks all files and saves them to a list, practical there are 2 errors:

  1. The listing is html formatted
  2. I only need the filename and extension

Code

string localPath = System.Reflection.Assembly.GetExecutingAssembly().Location;    
List<string> FtpListing = new List<string>();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(Properties.Settings.Default.FtpUrl);
//request.Proxy = GlobalProxySelection.GetEmptyWebProxy();
request.Credentials = new NetworkCredential(Properties.Settings.Default.FtpUsername, Properties.Settings.Default.FtpPassword);
request.Method = WebRequestMethods.Ftp.ListDirectory;
using (StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream()))
{
    string fileName = streamReader.ReadLine();
    while (fileName != null)
    {
         FtpListing.Add(fileName);
         fileName = streamReader.ReadLine();
    }
}

Without proxy it returns html, with the proxy statement uncommented I am getting a The remote server returned an error: (550) File unavailable (e.g., file not found, no access). error.

Where am I failing here?

/edit: here is a screenshot of the list, where all files should be listed, but instead a complete html file is saved:

FtpResponse

4

2 回答 2

0

我通过使用HtmlAgilityPack找到了一个可行的解决方案。

由于我无法更改 html 响应,因此我将其更改List<string> FtpListing为简单的string. 使用这个字符串,我解析<a>了 webrequest 提供的 html 代码中的每个标签。

代码:

string HtmlResult = String.Empty;
Console.WriteLine("Starting listing of files....");
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(Properties.Settings.Default.FtpUrl);
request.Credentials = new NetworkCredential(Properties.Settings.Default.FtpUsername, Properties.Settings.Default.FtpPassword);
request.UsePassive = false;
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
using (Stream responsestream = response.GetResponseStream())
{
    using (StreamReader reader = new StreamReader(responsestream))
    {
        string _line = reader.ReadLine();
        while (_line != null && _line != String.Empty)
        {
            HtmlResult += _line;
            _line = reader.ReadLine();
        }
    }                    
}
//parse html output
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(HtmlResult);
foreach (HtmlAgilityPack.HtmlNode node in doc.DocumentNode.SelectNodes("//a[@href]"))
{
    if(node.InnerText.Contains(".txt")) FtpListing.Add(node.InnerText);
}
Console.WriteLine("{0} Files found", FtpListing.Count);
于 2013-08-23T09:54:37.390 回答
0

使用此代码返回某个 ftp 目录中的文件名列表

    System.Net.FtpWebRequest ftpRequest =    System.Net.FtpWebRequest)System.Net.WebRequest.Create(SourceDirectory);
            ftpRequest.Credentials = new System.Net.NetworkCredential(SourceFTPUserName, SourceFTPPassword);
               ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
              System.Net.FtpWebResponse response =        (System.Net.FtpWebResponse)ftpRequest.GetResponse();
        System.IO.StreamReader streamReader = new System.IO.StreamReader(response.GetResponseStream());

        List<string> directories = new List<string>();

        string line = streamReader.ReadLine();
        while (!string.IsNullOrEmpty(line))
        {
            directories.Add(line);
            line = streamReader.ReadLine();
        }

        streamReader.Close();
        return directories;

希望能帮助到你。

于 2014-12-11T13:50:45.740 回答