FIX - System.Net.WebException: Remote server returned error: (500) Syntax error, command unrecognized

I have created FTP code for file transfer. This code works fine, except that it sometimes causes a 500 error. 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 an error occurs when downloading the largest file, i.e. about 290 KB. All other files are smaller than this, and I do not get an exception from them. I do not know why this is happening. Can someone tell me why?

Aside, in case you notice some possibility to improve my code or a logical error, please indicate this as well. I'm really not looking for code reviews, but welcome it.

 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 
+6
source share
3 answers

When reading your question, I was suspicious that this was due to (or could be fixed) setting KeepAlive to false . Looking at SO - this question refers to the same issue and points to this: fooobar.com/questions/962217 / ...

Try to install:

 request.KeepAlive = false; 

With KeepAlive set to false , your connection will be closed at the end of each request . If you transfer a large number of files, this can be a problem, as it takes time to retransmit credentials, etc. The positive thing is to recreate the connection in a known / initial state, which should solve your problem (even if this is not the root cause).

To find out what happens if you can enable verbose logging on your server, you should see the last command issued before you see this error. This should give you a better idea of ​​what is going on. Found this thread saying almost the same thing.

Update:

If I read the bottom of the link that I wrote myself, I could respond even better, the command that was probably reissued is part of the login process (i.e. USER username ), and this is your likely problem:

The reason the scade is no longer valid is because WebRequest uses a lease that expires after a certain time. Unless you explicitly create an instance of the lease and specify its timeouts, FtpWebRequest appears to use the default timeout values. I believe what happens when the lease expires, FtpWebRequest will then try logging in again.

So, look here with the correct search:

suggests that the expected query is not infinite by default, as specified , but actually 10000 ms . Which seems like a pretty big discrepancy. So you can also try installing:

 request.Timeout = -1; 

And see if your bug fixes.


Don't really think that this could be your problem, so move it to the bottom:

Also, make sure your request.ReadWriteTimeout is suitable for the speed you see for a larger file. The default is 5 minutes , which would be quite a long time for 290k, so I expect this to not be the source of your error. Also, I would expect a closed connection error if this was a problem.

+5
source

I also encountered the same exception with FTPWebRequest as part of a custom MSBuild task ... fortunately, the task opened the UsePassive="false" parameter (which sets the UsePassive property of the UsePassive object). Changing the value to "true" fixed the problem. Hope this helps!

  • (Set UsePassive to) false if the data transfer process of the client application listens for a connection to the data port; otherwise true if the client should initiate a connection to the data port. The default value is true.
  • Setting the UsePassive property to true sends the "PASV" command to the server. This command requires the server to listen on the data port and wait for a connection, rather than initiating it when it receives a transfer command.
  • If UsePassive set to true, the FTP server may not send the file size, and the upload process can always be zero. If UsePassive set to false , the firewall may raise a warning and block the download of the file.
+3
source

For anyone else who has this problem, and the above does not seem to work, I found that when I hit the above error, it is due to my username or password. I am sure that there are many people who copy and paste from Excel to SQL, so because of this, SQL picked up that my username / password has a carriage return line (when you press enter).

My code actually took the username as "Myusername and vbcrlf".

Hope this helps someone if they have the same problem.

Hooray!

0
source

All Articles