0

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.


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

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

But it fails to request

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

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Chen Zen
  • 11
  • 1
  • 8

2 Answers2

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

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

This answer may explain: Getting directory listing over http

Community
  • 1
  • 1
PaulF
  • 6,673
  • 2
  • 18
  • 29
  • i am a noob and i can't find a way to list all files url to array or list if someone can help that'll be awesome – Chen Zen Jun 15 '15 at 19:38
  • If the site is also an FTP server (you refer to FTP web folder in your question) if you replace the HTTP URL with the correct FTP URL for that site then your solution may work. If the site is only an HTTP server, then the second link in my answer explains why that is a problem. – PaulF Jun 16 '15 at 08:13
0

The FtpWebRequest is for FTP protocol.

FTP protocol does not use port 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.

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

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992