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;
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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();
}
}
}
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;