我正在尝试连接到使用主动模式的远程 FTP 服务器。 我使用 .NET Core 3.1 实现了一个简单的 C# FTP 客户端
var filename = _ftpServerUrl + "/" + targetFilePath;
System.Net.FtpWebRequest request = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(filename);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Proxy = null;
request.Credentials = new NetworkCredential(username, password);
request.KeepAlive = false;
request.UsePassive = false;
request.UseBinary = true;
byte[] fileContents;
using (StreamReader sourceStream = new StreamReader(fileStream))
{
fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
}
request.ContentLength = fileContents.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(fileContents, 0, fileContents.Length);
}
在本地主机上的 VS 2019 中运行时效果非常好。 但是,当代码在 Azure 中作为 Azure 函数 (v3) 运行时,我从服务器收到以下响应:
500 Syntax error, command unrecognized
我了解 FTP 主动模式要求客户端将其公共(public) IP 地址发送到服务器,然后服务器将尝试打开与客户端的传入连接。
Azure Function 沙箱中有什么东西可以阻止这种情况吗?
我什至尝试使用具有静态公共(public) IP 的应用服务计划来运行我的 Azure Function,但没有任何运气。
是否有人成功使用 Azure 应用服务或功能的事件模式连接到 FTP 服务器?
最佳答案
FTP 服务器不“使用主动模式”。。 FTP 客户端可以选择使用主动模式。但在防火墙和NAT无处不在的今天,使用主动模式几乎不起作用。对于云服务、虚拟机以及 Azure 功能等特定虚拟服务来说更是如此。
FTP 主动模式需要传入连接。 Azure 防火墙和 NAT 阻止了这种情况。我预计运行 Azure 功能的 VM 不会转发/打开任何传入端口。他们为什么会这么做?它违背了 Azure 函数的目的。它们旨在运行短暂的功能,而不是服务器。在这方面,主动 FTP 模式是一个奇怪的野兽。几乎没有任何其他客户端功能需要传入端口。
允许传入连接(如果在 Azure 函数中可能的话)不是一个编程问题。对您的代码进行任何更改都无济于事。一般来说,您应该使用被动模式。
有关一些背景信息,请参阅我关于 network configuration needed for the active and passive FTP modes 的文章.
关于c# - 从 Azure 函数针对 FTP 服务器(主动模式)运行 C# FTP 客户端 - "500 Syntax error, command unrecognized",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66126902/