我创建了 FTP 代码来传输文件.此代码工作正常,但有时会导致错误 500.确切的错误是 -
I created FTP code to transfer files. This code works fine except that it sometimes causes an error 500. The exact error is -
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)时会发生错误.所有其他文件都少于此,我对它们也不例外.我不知道为什么会这样.谁能告诉我为什么?
I noticed that the error occurs when the biggest file is loaded, ie about 290 KB. All other files are less than this and i get no exception for them. I don't know why this happens. Can someone tell me why ?
顺便说一句,如果您发现我的代码有一些改进空间或逻辑错误,请同时提及.我并不是真的在寻找代码审查,但它是受欢迎的.
As an aside, in case you notice some room for improvement in my code or logical error, then please mention that as well. I am not really looking for code reviews, but its welcome.
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 设置为 有关(或可以通过以下方式纠正)错误代码>.看着 SO - 这个问题引用了同样的问题并指出它:https://stackoverflow.com/a/2071374/1803682
On reading your question I was suspicious this has to do with (or could be corrected by) setting the KeepAlive to false. Looking on SO - this question references the same problem and points to it as well: https://stackoverflow.com/a/2071374/1803682
尝试设置:
request.KeepAlive = false;
将 KeepAlive 设置为 false 您的连接将是 在每个请求结束时关闭.如果您要传输大量文件,这可能是一个问题 - 因为重新发送凭据等需要时间.好处是您以已知/初始状态重新创建连接,这应该可以解决您的问题(即使它不是根原因).
With KeepAlive set to false your connection will be closed at the end of each request. If you are transmitting a lot of files this could be an issue - as it takes time to resend credentials, etc. The upside is you recreate the connection in a known / initial state which should solve your problem (even if it is not the root cause).
要查看发生了什么,如果您可以在服务器上启用详细日志记录,您应该会在看到返回此错误之前看到发出的最后一条命令.这应该让您更好地了解发生了什么.发现这个帖子说了很多同样的话.
To see what is going on, if you can enable detailed logging on your server you should see the last command issued before seeing this error returned. This should give you a better idea of what is up. Found this thread saying much the same thing.
更新:
如果我阅读了我自己发布的链接的底部,我可能会回答得更好,重新发出的命令可能是登录过程的一部分(即 USER 用户名),这是您可能遇到的问题:
If I had read to the bottom of the link I posted myself I could have answered even better, the command probably being reissued is some part of the login process (i.e. USER username) and this is your likely issue:
凭证可能不再有效的原因是WebRequest 使用在一定时间后到期的租约.如果您没有明确实例化租约并定义其超时,FtpWebRequest 似乎使用默认超时值.我相信发生的事情是,当租约到期时,FtpWebRequest 将然后尝试重新登录.
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.
看起来 这里 使用正确的搜索:
So looking here with the right search:
导致等待请求的默认超时不是无限的,因为 指定,但实际上10000 ms.这似乎是一个很大的差异.所以你也可以尝试设置:
yields that the default timeout waiting for requests is not infinite as specified but actually 10000 ms. Which seems a pretty big discrepancy. So you can also try setting:
request.Timeout = -1;
看看它是否能纠正你的错误.
And see if it corrects your error.
真的不认为这可能是您的问题,所以将其移至底部:
Really don't think this could be your issue so moving it to the bottom:
另外 - 检查您的 request.ReadWriteTimeout 是否适合您看到的较大文件的速度.默认值为 5分钟 这对于 290k 来说会很长,所以我希望这不是你的错误的根源.另外 - 如果这是问题,我预计会出现连接关闭错误.
Also - check that your request.ReadWriteTimeout is appropriate for the speed you see for the larger file. The default is 5 minutes which would be pretty long for 290k, so I expect this is not the source of your error. Also - I would expect a connection closed error if this was the problem.
这篇关于修复 - System.Net.WebException:远程服务器返回错误:(500)语法错误,命令无法识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
ASP.NET Core 使用 Azure Active Directory 进行身份验证并ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 进行身
ASP.NET Core 2.0 Web API Azure Ad v2 令牌授权不起作用ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授权不起作用)
ASP Core Azure Active Directory 登录使用角色ASP Core Azure Active Directory Login use roles(ASP Core Azure Active Directory 登录使用角色)
如何获取守护进程或服务器到 C# ASP.NET Web API 的How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何获取守护进程或服务器到 C# ASP.N
.Net Core 2.0 - 获取 AAD 访问令牌以与 Microsoft Graph.Net Core 2.0 - Get AAD access token to use with Microsoft Graph(.Net Core 2.0 - 获取 AAD 访问令牌以与 Microsoft Graph 一起使用)
异步调用时 Azure KeyVault Active Directory AcquireTokenAAzure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(异步调用时 Azure KeyVault Active Directory AcquireTokenAsync 超