I have built a script for downloading files from an FTP server. The script is working well with all the servers I have tested expect for one. For this server I am able to get the directory listing, but when I attempt to download a file it fails and returns a "421 Service Not Available, closing control connection" error. Below is the code I am using for downloading a file.
function Get-FtpFile
{
Param ([string]$fileUrl, $credentials, [string]$destination)
try
{
$FTPRequest = [System.Net.FtpWebRequest]::Create($fileUrl)
if ($credentials)
{
$FTPRequest.Credentials = $credentials
}
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$FTPRequest.UseBinary = $true
$FTPRequest.UsePassive = $true
# Send the ftp request
$FTPResponse = $FTPRequest.GetResponse()
# Get a download stream from the server response
$ResponseStream = $FTPResponse.GetResponseStream()
# Create the target file on the local system and the download buffer
$LocalFile = New-Object IO.FileStream ($destination,[IO.FileMode]::Create)
[byte[]]$ReadBuffer = New-Object byte[] 1024
# Loop through the download
do {
$ReadLength = $ResponseStream.Read($ReadBuffer,0,1024)
$LocalFile.Write($ReadBuffer,0,$ReadLength)
}
while ($ReadLength -ne 0)
$ResponseStream.Close()
$ReadBuffer.clear()
$LocalFile.Close()
$FTPResponse.Close()
}
catch [Net.WebException]
{
return "Unable to download because: $($_.exception)"
}
}
I am able to download files from this server using the Windows File Explorer FTP so I do not think this is a problem with the server itself.
One interesting thing I have noticed through testing is that when the server returns the directory listing each filename has a trailing NULL included. I have tried downloading files both including and not including this trailing NULL, both attempts produce the same error code.
Has anyone seen a similar error before? Or is anyone aware of the differences in the way that Windows File Explorer uses FTP compared with the PowerShell script I have listed above?