What is wrong with my ftp code?

I am using C # in .NEt 2.0 to just try to upload a file. Everything looks fine in the code, but it continues to fail when I go to create a stream from the FtpWebRequest.GetRequestStream method.

Here is the code ...

FtpWebRequest ftpRequest; FtpWebResponse ftpResponse; try { string fileName = Path.GetFileName(strCompleteFilePath); ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myhost/" + fileName)); ftpRequest.Method = WebRequestMethods.Ftp.UploadFile; ftpRequest.Proxy = null; ftpRequest.UseBinary = true; ftpRequest.Credentials = new NetworkCredential("myUserID", "myPW"); ftpRequest.KeepAlive = false; FileInfo ff = new FileInfo(strCompleteFilePath); byte[] fileContents = new byte[ff.Length]; using (FileStream fr = ff.OpenRead()) { fr.Read(fileContents, 0, Convert.ToInt32(ff.Length)); } using (Stream writer = ftpRequest.GetRequestStream()) { writer.Write(fileContents, 0, fileContents.Length); } ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); } 

And the mistake ....

 {System.Net.WebException: The remote server returned an error: (501) Syntax error in parameters or arguments. at System.Net.FtpWebRequest.SyncRequestCallback(Object obj) at System.Net.FtpWebRequest.RequestCallback(Object obj) at System.Net.CommandStream.InvokeRequestCallback(Object obj) at System.Net.CommandStream.Abort(Exception e) at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage) at System.Net.FtpWebRequest.GetRequestStream() 
+4
source share
5 answers

You do not have enough // paths.

You are about to create the path ftp://myhostmyfile.txt if your file was called "myfile.txt", which I assume should be ftp://myhost/myfile.txt

So just add a / at the end of the ftp://myhost .

+5
source

This looks wrong:

 ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myhost" + fileName)); 

If the contents of the file name starts with /, I think you need to add one of them so that it looks like this:

 ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myhost/" + fileName)); 
+4
source

Line:

 ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myhost" + fileName)); 

A problem may arise if your variable fileName does not include the necessary slashes.

+3
source

The FTP server is unhappy with the STOR command that .NET generates. The best place to look is in the log file for the server. Taking a wild guess: the path is unusual, typically you want to specify a directory name (e.g. ftp: // myhost / somedir / filename )

+1
source

Try

 ftpRequest.UsePassive = false; 

he works for me.

+1
source

Source: https://habr.com/ru/post/1308899/


All Articles