Windows Server 2019 - C# - FtpWebRequest dont work

Christian Erdtmann 1 Reputation point
2022-02-21T19:44:07.377+00:00

Hey i have bought a Windows Server 2019 on Strato.

Now i want to download fiels from an extern ftp to this server via. my Application.

The Application works fine on my local PC. It connects to the ftp and download all files.

But on the Windows Server i alltimes get a timerout error at response = reqFTP.GetResponse();

If i use filezilla on the Windows Server it works. I can manually download the files.

Additionla Info: I have now deaktivated my firewall and it works. Any idea what i need to do in the firewall that it works with an active firewall? - Adding the Programm to the rules was not successfull

I think its something with the server because the code is working local but here is my code:

public void GetFileListAndContinue(string RemoteDirectory, string sLocalDirectory)
        {
            string sActualRemoteDirectory = RemoteDirectory;
            string sActualDirectory = sLocalDirectory;

            string[] downloadFiles;
            StringBuilder result = new StringBuilder();
            WebResponse response = null;
            StreamReader reader = null;
            try
            {
                FtpWebRequest reqFTP;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(RemoteDirectory));
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(sFtpUserID, sFtpPassword);
                reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                reqFTP.Proxy = null;
                reqFTP.KeepAlive = false;
                reqFTP.UsePassive = false;
                response = reqFTP.GetResponse();
                reader = new StreamReader(response.GetResponseStream());
                string line = reader.ReadLine();
                while (line != null)
                {
                    result.Append(line);
                    result.Append("\n");
                    line = reader.ReadLine();
                }
                // to remove the trailing '\n'
                result.Remove(result.ToString().LastIndexOf('\n'), 1);

                string[] files = result.ToString().Split('\n');

                string item = ".";

                files = files.Where(e => e != item).ToArray();
                Console.WriteLine(String.Join(",", files));

                item = "..";

                files = files.Where(e => e != item).ToArray();
                Console.WriteLine(String.Join(",", files));

                foreach (string file in files)
                {
                    if (file.Contains("."))
                    {
                        Download(file, RemoteDirectory, sLocalDirectory);
                    }
                    else
                    {
                        sActualDirectory = sLocalDirectory;
                        sActualRemoteDirectory = RemoteDirectory;
                        sActualDirectory = sActualDirectory + file + "/";
                        sActualRemoteDirectory = sActualRemoteDirectory + file + "/";
                        createdir(sActualDirectory);

                        GetFileListAndContinue(sActualRemoteDirectory, sActualDirectory);
                    }
                }
            }
            catch (Exception ex)
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (response != null)
                {
                    response.Close();
                }
            }
        }
Internet Information Services
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,838 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Christian Erdtmann 1 Reputation point
    2022-02-21T21:05:09.793+00:00

    I got an answere here https://www.mcseboard.de/topic/221325-windows-server-2019-c-ftpwebrequest-dont-work/ and the solution is to use:

    reqFTP.UsePassive = true;


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.