服务器响应 PASV 命令返回的地址与建立 FTP 连接的地址不同

发布于 2024-08-30 17:43:32 字数 1334 浏览 8 评论 0原文

System.Net.WebException:服务器响应 PASV 命令返回的地址与建立 FTP 连接的地址不同。
在 System.Net.FtpWebRequest.CheckError()
在 System.Net.FtpWebRequest.SyncRequestCallback(对象 obj)
在 System.Net.CommandStream.Abort(异常 e)
在 System.Net.FtpWebRequest.FinishRequestStage(RequestStage 阶段)
在 System.Net.FtpWebRequest.GetRequestStream()
在 D:\PROJEKTI\BackupDB\BackupDB\Program.cs 中的 BackupDB.Program.FTPUploadFile(String serverPath, String serverFile, FileInfo LocalFile, NetworkCredential Cred):第 119 行

行代码:

FTPMakeDir(new Uri(serverPath + "/"), Cred);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath+serverFile);
request.UsePassive = true;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = Cred;
byte[] buffer = new byte[10240];    // Read/write 10kb

using (FileStream sourceStream = new FileStream(
    LocalFile.ToString(), FileMode.Open))
{
    using (Stream requestStream = request.GetRequestStream())
    {
        int bytesRead;
        do
        {
            bytesRead = sourceStream.Read(buffer, 0, buffer.Length);
            requestStream.Write(buffer, 0, bytesRead);
        } while (bytesRead > 0);
    }
    response = (FtpWebResponse)request.GetResponse();
    response.Close();
}

System.Net.WebException: The server returned an address in response to the PASV command that is different than the address to which the FTP connection was made.
at System.Net.FtpWebRequest.CheckError()
at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
at System.Net.CommandStream.Abort(Exception e)
at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
at System.Net.FtpWebRequest.GetRequestStream()
at BackupDB.Program.FTPUploadFile(String serverPath, String serverFile, FileInfo LocalFile, NetworkCredential Cred) in D:\PROJEKTI\BackupDB\BackupDB\Program.cs:line 119

code:

FTPMakeDir(new Uri(serverPath + "/"), Cred);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath+serverFile);
request.UsePassive = true;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = Cred;
byte[] buffer = new byte[10240];    // Read/write 10kb

using (FileStream sourceStream = new FileStream(
    LocalFile.ToString(), FileMode.Open))
{
    using (Stream requestStream = request.GetRequestStream())
    {
        int bytesRead;
        do
        {
            bytesRead = sourceStream.Read(buffer, 0, buffer.Length);
            requestStream.Write(buffer, 0, bytesRead);
        } while (bytesRead > 0);
    }
    response = (FtpWebResponse)request.GetResponse();
    response.Close();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

jJeQQOZ5 2024-09-06 17:43:32

我的天啊。这里所有的迎合购买他们的第 3 方解决方案而不是通知您更改一行代码是怎么回事?

尝试切换被动值以查看哪个有效:

    request.UsePassive = false;

这可能取决于机器(客户端和服务器)之间的防火墙。

我注意到如果我通过我们的防火墙,那么我需要将其保留为 True,否则它将返回异常:

远程服务器返回错误:(500) 语法错误,命令
无法识别。

但是,如果我位于防火墙后面(例如数据中心内的两台机器直接相互连接),那么我需要将其设置为 False,否则它将返回异常:

服务器响应 PASV 命令返回的地址与 FTP 连接的地址不同
制作。

如果这有效并且您想让您的解决方案更具适应性,您可以使用默认的 True 值将您的请求包装在 try-catch 块中,如果您收到 500 错误,则将 UsePassive 切换为 False 并重试。

omg. What's up with all the pandering here for buying their 3rd party solutions instead of informing you to change one line of code?

Try toggling the Passive value to see which works:

    request.UsePassive = false;

This may depend on the firewall between the Machines (client and server).

I've noticed if I go through our firewall, then I need it left at True, otherwise it will return the Exception:

The remote server returned an error: (500) Syntax error, command
unrecognized.

However, if I'm behind the firewall (like two machines connecting directly to each other within a data-center) then I need to set it to False, otherwise it will return the Exception:

The server returned an address in response to the PASV command that is different than the address to which the FTP connection was
made.

If this works and you want to make your solution more adaptable, you could wrap your request in a try-catch block using the default True value, and if you get the 500 error, then switch UsePassive to False and try it again.

隐诗 2024-09-06 17:43:32

如果有人遇到同样的问题,这是 proftpd

http://www.proftpd 的解决方案。 org/docs/howto/NAT.html

if anyone have the same problem, this is solution for proftpd

http://www.proftpd.org/docs/howto/NAT.html

甜尕妞 2024-09-06 17:43:32

在被动模式下,FTP 会话如下:

client: PASV
(i would like to transfer files. Tell me which port and ip address should I use)

server: 227 Entering Passive Mode (172,16,3,4,204,173)
(ok, use port 52397 on IP address 172.16.3.4.)

client: connects to this IP address/port and starts data transfer.

看起来具有两个公共 IP 地址(例如 1.2.3.4)的 FTP 服务器返回一个私有 IP 地址作为对 PASV 命令的响应。

解决方案

切换到主动模式。

在主动模式下,FTP 服务器连接到 FTP 客户端进行数据传输。它可以解决这个问题,但对防火墙不友好。当传入连接被阻止时(很常见),它将不起作用。

忽略作为 PASV 命令响应发送的 IP 地址

如果公共 ftp 服务器 IP 地址是公共 IP 地址,并且作为 PASV 命令响应返回的 IP 地址来自私有范围(例如 10., 192.168. )。在这种情况下,FTP 客户端应使用公共 IP 地址。

这正是我们的 Rebex FTP 在这种情况下所做的事情。它运行良好(可以关闭此行为)。甚至可以为具有多个公共 IP 地址的服务器打开它。

我不知道 FtpWebRequest 是否可以实现类似的解决方法。

您可以下载试用版并检查它是否可以解决您的问题。

In passive mode FTP conversation goes as follows:

client: PASV
(i would like to transfer files. Tell me which port and ip address should I use)

server: 227 Entering Passive Mode (172,16,3,4,204,173)
(ok, use port 52397 on IP address 172.16.3.4.)

client: connects to this IP address/port and starts data transfer.

It looks like the FTP server with two public IP address (e.g. 1.2.3.4) returns a private IP address as a response to PASV command.

Solution

Switching to Active mode.

In active mode FTP server connects to FTP client for data transfers. It would solve this issue, but is not firewall friendly. It will not work when incoming connections are blocked (very common).

Ignoring IP address send as response to PASV command

If the public ftp server IP address is a public one, and IP address returned as a response for PASV command is from private range (such as 10., 192.168.). In such case the FTP client should use the public IP address.

This is exactly what does our Rebex FTP do in such situation. It works well (this behavior can be switched off). It can be even turned on for servers with multiple public IP addresses.

I don't know whether similar workaround is possible with FtpWebRequest.

You can download trial and check whether it solves your problem.

情深已缘浅 2024-09-06 17:43:32

经过大量研究后,我发现解决此问题的唯一方法是更改​​服务器上的 PASV 设置。

幸运的是,我控制了客户端和服务器计算机,因此我能够告诉服务器(在我的例子中是 FileZilla)使用公共 IP 而不是私有 IP。

After a lot of digging around I found the only way to solve this problem was to change the PASV settings on the server.

Fortunately I control both the client and server machines so I was able to tell the server (FileZilla in my case) to use the public IP rather than the private IP.

怂人 2024-09-06 17:43:32

您的 FTP 服务器配置错误。

在被动模式下,服务器报告客户端应连接以进行数据传输的 IP 地址和端口。您的 FTP 服务器报告其内部网络中的 IP 地址,尽管它位于防火墙/NAT 后面。由于明显的原因,客户端无法连接到该内部地址。您必须配置 FTP 服务器以报告其外部 IP 地址。

具体如何完成是特定于服务器的,您没有告诉我们您的 FTP 服务器是什么。


这里的一些答案建议使用主动模式。

request.UsePassive = false;

但是,只有在客户端和服务器之间没有防火墙/NAT 的情况下,这才有用,在这种情况下,您一开始就不会遇到问题(除非服务器真的损坏并报告完全错误的 IP 地址,而不仅仅是内部一)。或者,如果客户端的防火墙/NAT 配置为允许入站连接,则不常见。


另一种方法是使用不同的 FTP 库,该库可以通过忽略服务器报告的不正确的 IP 地址并使用主/控制连接 IP 地址来解决该问题。或者使用 EPSV 命令而不是隐式使用主/控制连接 IP 地址的 PASV 命令。

Your FTP server is misconfigured.

In the passive mode the server reports an IP address and port to which the client should connect to for a data transfer. Your FTP server reports its IP address within an internal network, although it's behind a firewall/NAT. The client cannot connect to that internal address for obvious reason. You have to configure the FTP server to report its external IP address.

How that's done is server-specific and you didn't tell us, what your FTP server is.


Some answer here suggest using the active mode.

request.UsePassive = false;

But that can help only if there's no firewall/NAT between the client and the server, in which case you won't have the problem in the first place (unless the server is really broken and reports a completely wrong IP address, not only the internal one). Or if the client's firewall/NAT is configured to allow inbound connections, what is not usual.


Another approach is using a different FTP library that can workaround the problem by ignoring the incorrect IP address reported by the server and using the primary/control connection IP address. Or by using the EPSV command instead of PASV command that implicitly uses the primary/control connection IP address.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文