C# FTP错误227进入被动模式
我正在尝试将文件上载到ftp服务器。尝试了一些代码示例,但总是出现此错误,进入被动模式。例如,我可以用以下代码创建一个目录C# FTP错误227进入被动模式,c#,ftp,ftpwebrequest,C#,Ftp,Ftpwebrequest,我正在尝试将文件上载到ftp服务器。尝试了一些代码示例,但总是出现此错误,进入被动模式。例如,我可以用以下代码创建一个目录 FtpWebRequest reqFTP; try { // dirName = name of the directory to create. reqFTP = (FtpWebRequest)FtpWebRequest.Create( new Uri("ftp://" + ftpServerIP + "/" + dirName)
FtpWebRequest reqFTP;
try
{
// dirName = name of the directory to create.
reqFTP = (FtpWebRequest)FtpWebRequest.Create(
new Uri("ftp://" + ftpServerIP + "/" + dirName));
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.UsePassive = false;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
ftpStream.Close();
response.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(
"ftp://" + ftpServerIP + "/" + fileInf.Name));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
try
{
Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error");
}
或者,例如,我可以重命名一个文件。但无法使用此代码上载文件
FtpWebRequest reqFTP;
try
{
// dirName = name of the directory to create.
reqFTP = (FtpWebRequest)FtpWebRequest.Create(
new Uri("ftp://" + ftpServerIP + "/" + dirName));
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.UsePassive = false;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
ftpStream.Close();
response.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(
"ftp://" + ftpServerIP + "/" + fileInf.Name));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
try
{
Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error");
}
在reqFTP.GetRequestStream()
处获取异常
如果我使用reqFTP.usesponsive=false
则我得到“
远程服务器返回错误:(500)语法错误,无法识别命令”
我该怎么办?试试这个例子
如果将UseAnsive设置为false,则需要确保命令通道的端口已打开(即,需要定义端点和访问规则)。除非有充分的理由不使用被动语态,否则你最好使用被动语态
希望这会有所帮助。所以,我知道这是一个迟来的答案,但我想分享我的经验,以防有人会有同样的问题 在我的例子中,我从Windows Server 2016下载了一些文件。出于某些安全原因,我激活了防火墙,当然我在防火墙中添加了一个入站规则,允许20和21个端口 我犯了同样著名的错误:227进入被动模式。 我检查了代码,在错误中,它总是指示一些不同的端口 经过一些搜索,我发现我必须在允许的端口中添加49152-65534 它成功了 这是我的密码
public static bool DownloadDocument(string ftpPath, string downloadPath)
{
bool retVal = false;
try
{
if (!Directory.Exists(Tools.LocalPath))
Directory.CreateDirectory(Tools.LocalPath);
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential(Tools.FtpUserName, Tools.FtpPassword);
client.DownloadFile(ftpPath, downloadPath);
}
retVal = true;
}
catch (Exception ex)
{
UserMethods.ParseError(ex, "DownloadFile");
}
return retVal;
}
正如我所说的,如果我使用reqFTP.usesponsive=false,那么我会得到“远程服务器返回了一个错误:(500)语法错误,命令无法识别”任何有此问题的人(或者发布一个新问题并链接到这里)。没有日志,这个问题无法回答。我今天遇到这个问题是因为卡巴斯基病毒。如果我暂停卡巴斯基并重试,一切都会按预期进行