Hey, ich habe einen Windows Server 2019 gekauft und versuche, das folgende C#-Programm auszuführen.
Auf meinem lokalen PC funktioniert alles einwandfrei, es verbindet sich und lädt alle Daten von meinem FTP.
Aber auf meinem lokalen PC verwende ich reqFTP.UsePassive = false;
Auf dem Windows - Server ist die Anfrage immer abgelaufen, wenn ich versuche, reqFTP.UsePassive = false zu verwenden; also habe ich reqFTP.UsePassive = true verwendet;
Dies war der erste Schritt.Das Programm startet nun connect und kopiert den ersten Ordner meiner Verzeichnisse.
Aber das ist alles. Es las alle Ordner und las den Inhalt des ersten Ordners.Aber wenn es versucht, die erste Datei aus dem ersten Verzeichnis herunterzuladen, bekomme ich wieder ein Timeout bei FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Zusätzliche Info: Wenn ich meine Firewall abschalte, dann funktioniert alles und die Dateien werden geladen:
Code unten:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace GetBotfilesFromFTP
{
class Program
{
string sFtpServerIP = "SERVERIP";
string sFtpUserID = "USERID";
string sFtpPassword = "USERPASSWORT";
string sRemoteDir = "ftp://w01a18b1.kasserver.com/";
static void Main(string[] args)
{
Program self = new Program();
string url = "ftp://" + self.sFtpServerIP + "/";
NetworkCredential credentials = new NetworkCredential();
credentials.Password = self.sFtpPassword;
credentials.UserName = self.sFtpUserID;
self.GetFileListAndContinue(self.sRemoteDir, "C:/");
}
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 = true;
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();
}
}
}
private void Download(string file, string RemoteDirectory, string sLocalPath)
{
try
{
string uri = RemoteDirectory + file;
Uri serverUri = new Uri(uri);
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return;
}
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(sFtpUserID, sFtpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Proxy = null;
reqFTP.UsePassive = false;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream responseStream = response.GetResponseStream();
FileStream writeStream = new FileStream(sLocalPath + file, FileMode.Create);
int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, Length);
}
writeStream.Close();
response.Close();
}
catch (WebException wEx)
{
}
catch (Exception ex)
{
}
}
//// In this part i create the sub-directories.
///
public void createdir(string path)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
}
}