固定 - System.Net.WebException:远程服务器返回错误:(500)语法错误,命令无法识别 [英] Fixing - System.Net.WebException: The remote server returned an error: (500) Syntax error, command unrecognized

查看:7116
本文介绍了固定 - System.Net.WebException:远程服务器返回错误:(500)语法错误,命令无法识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建FTP密码来传输文件。此代码的工作,除了它有时会导致错误500确切的错误是好的 -

 错误:System.Reflection.TargetInvocationException:例外
被调用的目标引发异常。
---> System.Net.WebException:远程服务器返回错误:
(500)语法错误,不可识别的命令。
在System.Net.FtpWebRequest.CheckError()
在System.Net.FtpWebRequest.SyncRequestCallback(obj对象)
在System.Net.CommandStream.Abort(例外五)
在System.Net.FtpWebRequest.FinishRequestStage(RequestStage阶段)的System.Net.FtpWebRequest.GetRequestStream
()在ST_772dn22cj49ndfddatee.csproj.ScriptMain.Main
()
---的内端异常堆栈跟踪---

我注意到,当最大的文件被加载,即约出现错误290 KB。所有其他文件都小于这一点,我没有得到任何例外他们。我不知道为什么会这样。谁能告诉我,为什么?



顺便说一句,如果你发现一些空间,在我的代码或逻辑错误的改善,那么请提及这一点。我不是真的找代码审查,但其受欢迎的。

 公共无效的主要()
{

布尔条件=真;

如果(条件== true)而
{
串fileLocation =我的windows目录;
字符串文件名=fileName.extension;

串ftpFolder = @/ ftpFolder /;
布尔ftpMode = TRUE; //被动或主动的。真=被动
串ftpPassword =密码;
INT ftpPort = 21; //默认
串ftpServerName =服务器名称;
串ftpUserName =用户名;

//创建一个对象与服务器进行通信。
串ftpRequestString =FTP://+ ftpServerName +:
+ ftpPort + ftpFolder +文件名;

尝试{

的FtpWebRequest请求=
(的FtpWebRequest)WebRequest.Create(ftpRequestString);
request.Method = WebRequestMethods.Ftp.UploadFile;

request.Credentials =新的NetworkCredential(ftpUserName,ftpPassword);

//设置模式
如果(ftpMode ==真){
request.UsePassive = TRUE;
}

//将文件复制到该请求。

字符串文件路径= @fileLocation +\\+文件名;
的StreamReader的SourceStream =新的StreamReader(文件路径);
字节[] = fileContents Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;

流requestStream = request.GetRequestStream();
requestStream.Write(fileContents,0,fileContents.Length);
requestStream.Close();

FtpWebResponse响应=(FtpWebResponse)request.GetResponse();

response.Close();

}
赶上(引发WebException前)
{
MessageBox.Show(ex.Message);

} //的try-catch

}

} //主


解决方案

在读你的问题我很怀疑这有做(或者可以通过纠正)设置的KeepAlive 。看好SO - 这个问题引用了同样的问题,并指出它还有: http://stackoverflow.com/a/2071374/1803682



尝试设置:

  request.KeepAlive =假; 

通过的KeepAlive 设置为您的连接会的收于每个请求的结束。如果您传输大量文件,这可能是一个问题 - 因为它需要时间来重新发送凭证等的好处是你重新创建一个已知/初始状态应该解决您的问题(即使它不是根连接引起)。



要看看到底是怎么回事,如果你能在服务器上启用详细日志,你应该看到看到此错误返回前发出的最后一个命令。这应该给你一个什么样是一个更好的主意。发现这个线程多说了同样的事情。



更新:



如果我读给我贴我的链接的底部我本来可以更好的回答,可能被补发的命令是在登录过程中的某些部分(即 user用户名),这是你的可能的问题:




究其原因,可能creadentials不再有效的是,
的WebRequest使用了一定时间后到期的租约。
如果你不明确B中的FtpWebRequest似乎使用默认的超时值实例租约并定义其超时,$ B $。我相信,
发生的事情是,当租约到期的FtpWebRequest将
然后尝试重新登录。




所以找的这里用正确的搜索



收益率的默认超时等待请求不是无限的的指定但实际上 10000毫秒。这似乎一个相当大的差异。所以,你也可以尝试设置:

  request.Timeout = -1; 

和看它是否纠正你的错误。



< HR>

真的不认为这可能是你的问题,所以它移到底部:



<击>此外 - 检查你的 request.ReadWriteTimeout 是适合您将看到较大的文件的速度。默认值是5分钟这将是290K很长,所以我希望这不是你的错误的根源。此外 - 我希望一个连接关闭错误,如果这是问题


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 --- 

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

解决方案

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: http://stackoverflow.com/a/2071374/1803682

Try setting:

request.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.

Update:

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:

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:

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:

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)语法错误,命令无法识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆