I have a test C# console app that uses FtpWebRequest
to transfer a file to a public FTP server.
The app works from my Dev PC with UsePassive set to False. However, when I copy the app to an Azure VM it raises the following exception:
The remote server returned an error: (500) Syntax error, command unrecognized.
So in summary, these are the results when running the app locally and on Azure:
UsePassive Local PC Azure VM
---------- -------- --------
True ok ok
False ok ERROR!
Enabling tracing for System.Net
shows the following (extract):
System.Net Information: 0 : [5716] FtpControlStream#60068066 - Created connection from 10.0.0.4:49706 to 90.130.70.73:21.
System.Net Information: 0 : [5716] Associating FtpWebRequest#45004109 with FtpControlStream#60068066
System.Net Information: 0 : [5716] FtpControlStream#60068066 - Received response [220 (vsFTPd 2.3.5)]
System.Net Information: 0 : [5716] FtpControlStream#60068066 - Sending command [USER anonymous]
System.Net Information: 0 : [5716] FtpControlStream#60068066 - Received response [331 Please specify the password.]
System.Net Information: 0 : [5716] FtpControlStream#60068066 - Sending command [PASS ********]
System.Net Information: 0 : [5716] FtpControlStream#60068066 - Received response [230 Login successful.]
System.Net Information: 0 : [5716] FtpControlStream#60068066 - Sending command [OPTS utf8 on]
System.Net Information: 0 : [5716] FtpControlStream#60068066 - Received response [200 Always in UTF8 mode.]
System.Net Information: 0 : [5716] FtpControlStream#60068066 - Sending command [PWD]
System.Net Information: 0 : [5716] FtpControlStream#60068066 - Received response [257 "/"]
System.Net Information: 0 : [5716] FtpControlStream#60068066 - Sending command [TYPE I]
System.Net Information: 0 : [5716] FtpControlStream#60068066 - Received response [200 Switching to Binary mode.]
System.Net Information: 0 : [5716] FtpControlStream#60068066 - Sending command [PORT 10,0,0,4,194,43]
System.Net Information: 0 : [5716] FtpControlStream#60068066 - Received response [500 Illegal PORT command.]
System.Net Information: 0 : [5716] FtpWebRequest#45004109::(Releasing FTP connection#60068066.)
System.Net Error: 0 : [5716] Exception in FtpWebRequest#45004109::GetRequestStream - The remote server returned an error: (500) Syntax error, command unrecognized..
at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
Here is the source code to my app:
var fileContents = Encoding.UTF8.GetBytes("this is a test");
Console.Write("UsePassive? (y/n)");
var passive = Console.ReadKey();
var usePassive = passive.Key == ConsoleKey.Y;
var request = (FtpWebRequest)WebRequest.Create("ftp://speedtest.tele2.net/upload/mytest.txt");
request.Credentials = new NetworkCredential("anonymous", "anonymous");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UsePassive = usePassive;
request.ContentLength = fileContents.Length;
using (var requestStream = request.GetRequestStream())
{
requestStream.WriteAsync(fileContents, 0, fileContents.Length);
}
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
Console.WriteLine($"Upload File Complete, status {response.StatusDescription}");
}
The Azure VM is not part of a network security group. I've tried disabling the firewall on the VM but the app still raises the exception.
How can I resolve this? It's a requirement that my app has UsePassive
set to False
.