从Web(端口80)上的FTP文件夹到阵列获取列表

时间:2022-07-28 03:46:18

I'm trying to get a list of files from FTP web folder via port 80 into an array or list and then download specific extensions, but a counter is always zero for some reason.

我正在尝试通过端口80将FTP文件夹中的文件列表添加到数组或列表中,然后下载特定的扩展名,但由于某种原因,计数器始终为零。


public void GetFilesFromServer(string url, string extension=".mp3")
{
    List<string> files = new List<string>();

    try
    {
        //Create FTP request
        FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(url);

        request.Method = WebRequestMethods.Ftp.ListDirectory;
        request.Credentials = new NetworkCredential("anonymous", "anonymous");
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = false;

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);

        while (!reader.EndOfStream)
        {
            files.Add(reader.ReadLine());
        }

        //Clean-up
        reader.Close();
        responseStream.Close(); //redundant
        response.Close();
    }
    catch (Exception)
    {
       // MessageBox.Show("There was an error connecting to the FTP Server");
    }

    //If the list was successfully received, display it to the user
    //through a dialog
    if (files.Count != 0)
    {
        string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DownloadedFiles");
        WebClient wc = new WebClient();
        foreach (string file in files)
        {
            if(file.EndsWith(extension))
                wc.DownloadFile(url+"/"+file, Path.Combine(folderPath, file));
        }
    }
}

My goal is to put all .ext files into an array and I can't get the list

我的目标是将所有.ext文件放入一个数组中,但我无法获取列表

The folder is http://url.com/folder for example.

例如,该文件夹是http://url.com/folder。

But it fails to request

但它没有要求

I checked the solutions and it doesn't work for me.

我检查了解决方案,它对我不起作用。

2 个解决方案

#1


0  

The FtpWebRequest is for FTP protocol.

FtpWebRequest用于FTP协议。

FTP protocol does not use port 80.

FTP协议不使用端口80。

If your URL is an HTML presentation of a folder on the server, you cannot scrape it with FtpWebRequest. You have to parse the HTML.

如果您的URL是服务器上文件夹的HTML表示,则无法使用FtpWebRequest进行删除。你必须解析HTML。

Though you should better find a way to access the data using a more reliable method (such as using a real FTP).

虽然您最好使用更可靠的方法(例如使用真实的FTP)找到访问数据的方法。

#2


0  

The MS documentation says that the FtpWebRequest initializes a new WebRequest and according to the documentation here : https://msdn.microsoft.com/en-us/library/bw00b1dc(v=vs.110).aspx

MS文档说FtpWebRequest初始化一个新的WebRequest并根据这里的文档:https://msdn.microsoft.com/en-us/library/bw00b1dc(v = vs.110).aspx

then if the URL starts with "http://" you will get back an HttpWebRequest rather than a FtpWebRequest.

然后,如果URL以“http://”开头,您将返回HttpWebRequest而不是FtpWebRequest。

This answer may explain: Getting directory listing over http

这个答案可能解释为:通过http获取目录列表

#1


0  

The FtpWebRequest is for FTP protocol.

FtpWebRequest用于FTP协议。

FTP protocol does not use port 80.

FTP协议不使用端口80。

If your URL is an HTML presentation of a folder on the server, you cannot scrape it with FtpWebRequest. You have to parse the HTML.

如果您的URL是服务器上文件夹的HTML表示,则无法使用FtpWebRequest进行删除。你必须解析HTML。

Though you should better find a way to access the data using a more reliable method (such as using a real FTP).

虽然您最好使用更可靠的方法(例如使用真实的FTP)找到访问数据的方法。

#2


0  

The MS documentation says that the FtpWebRequest initializes a new WebRequest and according to the documentation here : https://msdn.microsoft.com/en-us/library/bw00b1dc(v=vs.110).aspx

MS文档说FtpWebRequest初始化一个新的WebRequest并根据这里的文档:https://msdn.microsoft.com/en-us/library/bw00b1dc(v = vs.110).aspx

then if the URL starts with "http://" you will get back an HttpWebRequest rather than a FtpWebRequest.

然后,如果URL以“http://”开头,您将返回HttpWebRequest而不是FtpWebRequest。

This answer may explain: Getting directory listing over http

这个答案可能解释为:通过http获取目录列表