我创建了 FTP 代码来传输文件。这段代码工作正常,除了它有时会导致错误 500。确切的错误是 -
Error: System.Reflection.TargetInvocationException: Exception has
been thrown by the target of an invocation.
---> System.Net.WebException: The remote server returned an error:
(500) Syntax error, command unrecognized.
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 ST_772dn22cj49ndfddatee.csproj.ScriptMain.Main()
--- End of inner exception stack trace ---
我注意到错误发生在加载最大文件时,即大约 290 KB。所有其他文件都小于这个,我也不异常(exception)。我不知道为什么会这样。谁能告诉我为什么?
顺便说一句,如果您注意到我的代码有一些改进空间或逻辑错误,那么也请提及。我并不是真的在寻找代码审查,但欢迎它。
public void Main()
{
Boolean conditions = true;
if(conditions == true)
{
string fileLocation = "my windows directory";
string fileName = "fileName.extension";
string ftpFolder = @"/ftpFolder/";
Boolean ftpMode = true; //passive or active. True = passive
string ftpPassword = "password";
int ftpPort = 21;// the default
string ftpServerName = "server name";
string ftpUserName = "user name";
//Create an object to communicate with the server.
string ftpRequestString = "ftp://" + ftpServerName + ":"
+ ftpPort + ftpFolder + fileName;
try{
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create(ftpRequestString);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
//Set mode
if(ftpMode == true){
request.UsePassive = true;
}
//Copy the file to the request.
string filePath = @fileLocation + "\\" + fileName;
StreamReader sourceStream = new StreamReader(filePath);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
}
catch (WebException ex)
{
MessageBox.Show(ex.Message);
}//try-catch
}
}//main
最佳答案
阅读您的问题后,我怀疑这与将 KeepAlive
设置为 false
有关(或可以通过以下方式纠正)。看看 SO - 这个问题引用了同样的问题并指向它:https://stackoverflow.com/a/2071374/1803682
尝试设置:
request.KeepAlive = false;
将 KeepAlive
设置为 false
,您的连接将为 closed at the end of each request .如果您正在传输大量文件,这可能是一个问题 - 因为重新发送凭据等需要时间。好处是您可以在已知/初始状态下重新创建连接,这应该可以解决您的问题(即使它不是根原因)。
要查看发生了什么,如果您可以在您的服务器上启用详细日志记录,您应该看到在看到此错误返回之前发出的最后一个命令。这应该让您更好地了解发生了什么。找到this thread说的差不多。
更新:
如果我阅读了我自己发布的链接的底部,我可能会回答得更好,重新发出的命令可能是登录过程的一部分(即 USER username
),这是您可能遇到的问题:
The reason the creadentials may no longer be valid is that the WebRequest uses a lease that expires after a certain amount of time. If you don't explicitly instantiate the lease and define its timeouts, the FtpWebRequest appears to use default timeout values. I believe what's happening is that when the lease expires the FtpWebRequest will then try to log on again.
这么看here使用正确的搜索:
导致等待请求的默认超时不是无限的 specified但实际上 10000 毫秒
。这似乎是一个很大的差异。所以你也可以尝试设置:
request.Timeout = -1;
看看它是否能纠正你的错误。
真的不认为这可能是你的问题所以把它移到底部:
此外 - 检查您的 request.ReadWriteTimeout
是否适合您看到的较大文件的速度。默认值为 5 minutes这对于 290k 来说会很长,所以我希望这不是您的错误来源。另外 - 如果这是问题所在,我预计会出现连接关闭错误。
关于c# - 修复 - System.Net.WebException : The remote server returned an error: (500) Syntax error, 命令无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21221300/