FTP connection failing with “Unable to connect to the remote server”
Posted: 2019-10-15 07:56
Hello,
I am experiencing some issues in my .NET Core 2.2 setup where I have a feature that needs to process a lot of files located on our VM. To retrieve the files, I make continous ftpwebrequests, with FileZilla as our FTP Server running on the VM.
The issue comes when I need to download hundreds of files using the FTPWebrequests, somewhere random between 300-400 requests, I get the error "Unable to connect to the remote server".
My code for retrieving the files looks like this:
I am experiencing some issues in my .NET Core 2.2 setup where I have a feature that needs to process a lot of files located on our VM. To retrieve the files, I make continous ftpwebrequests, with FileZilla as our FTP Server running on the VM.
The issue comes when I need to download hundreds of files using the FTPWebrequests, somewhere random between 300-400 requests, I get the error "Unable to connect to the remote server".
My code for retrieving the files looks like this:
Code: Select all
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(filepath);
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpRequest.Credentials = new NetworkCredential("username", "password");
if (finalProcessRequest)
{
ftpRequest.KeepAlive = false;
}
else
{
ftpRequest.KeepAlive = true;
}
Encoding encoding = Encoding.GetEncoding(Settings.StandardCodingPage);
if (useUTF8)
{
encoding = Encoding.UTF8;
}
if (codePage != -1)
{
encoding = Encoding.GetEncoding(codePage);
}
StringBuilder objectCode = new StringBuilder();
FtpWebResponse webResponse = (FtpWebResponse) ftpRequest.GetResponse();
Stream responseStream = webResponse.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, encoding);
string line;
while ((line = await reader.ReadLineAsync()) != null)
{
objectCode.AppendLine(line);
}
reader.Close();
webResponse.Close();
result = objectCode.ToString();