gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:59:28 29 4
gpt4 key购买 nike

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

代码:

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();
}

最佳答案

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

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

    request.UsePassive = false;

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

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

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

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

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

如果这行得通并且你想让你的解决方案更具适应性,你可以使用默认的 True 值将你的请求包装在一个 try-catch block 中,如果你得到 500 错误,然后将 UsePassive 切换为 False 并重试.

关于c# - 服务器响应 PASV 命令返回的地址与建立 FTP 连接的地址不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2709498/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com