服务器响应 PASV 命令返回的地址与建立 FTP 连接的地址不同
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我的天啊。这里所有的迎合购买他们的第 3 方解决方案而不是通知您更改一行代码是怎么回事?
尝试切换被动值以查看哪个有效:
这可能取决于机器(客户端和服务器)之间的防火墙。
我注意到如果我通过我们的防火墙,那么我需要将其保留为 True,否则它将返回异常:
但是,如果我位于防火墙后面(例如数据中心内的两台机器直接相互连接),那么我需要将其设置为 False,否则它将返回异常:
如果这有效并且您想让您的解决方案更具适应性,您可以使用默认的 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:
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:
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:
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.
如果有人遇到同样的问题,这是 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
在被动模式下,FTP 会话如下:
看起来具有两个公共 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:
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.
经过大量研究后,我发现解决此问题的唯一方法是更改服务器上的 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.
您的 FTP 服务器配置错误。
在被动模式下,服务器报告客户端应连接以进行数据传输的 IP 地址和端口。您的 FTP 服务器报告其内部网络中的 IP 地址,尽管它位于防火墙/NAT 后面。由于明显的原因,客户端无法连接到该内部地址。您必须配置 FTP 服务器以报告其外部 IP 地址。
具体如何完成是特定于服务器的,您没有告诉我们您的 FTP 服务器是什么。
MasqueradeAddress
指令。pasv_address
指令。这里的一些答案建议使用主动模式。
但是,只有在客户端和服务器之间没有防火墙/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.
MasqueradeAddress
directive.pasv_address
directive.Some answer here suggest using the active mode.
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 ofPASV
command that implicitly uses the primary/control connection IP address.