C # Absolute URI removes ".." from URL

I need to upload a file via FTP at ftp://ftp.remoteServer.com

My root directory on the remote server contains the upload and download folders. I need to put the file in the upload folder. But when you log into the system, the server automatically puts me in the "download" folder.

I tried to do this:

string serverTarget = "ftp://ftp.remoteServer.com/"; serverTarget += "../upload/myfile.txt"; Uri target = new Uri(serverTarget); FTPWebRequest ftp = (FTPWebRequest)FtpWebRequest.Create(target); using(Stream requestStream = ftp.GetRequestStream()) { // Do upload here } 

This code does not work: (550) The file is not accessible (for example, the file was not found, there is no access) I debugged the code, and target.AbsoluteUri returned as ftp://ftp.remoteServer.com/upload instead of ftp: //ftp.remoteServer. com /../ upload (missing ..)

If I put ftp://ftp.remoteServer.com/../upload in the browser, I can log in and check that this is the right place where I want to put the file,

How can I get FTPWebRequest to go to the right place?

+7
source share
3 answers

I believe that you can encode points as% 2E to save points in your URI.

So something like:

 string serverTarget = "ftp://ftp.remoteServer.com/%2E%2E/upload/myfile.txt"; 
+2
source

Try the following:

 string serverTarget = "../upload/myfile.txt"; Uri uri = new Uri(serverTarget, UriKind.Relative); 
+1
source

Andy Evans comment is correct.

Consider the URI: http://ftp.myserver.com/../ . .. means "take me to the parent directory". But no parent! Therefore, when you get the absolute URL, you will end with http://ftp.myserver.com/ The parser cannot do anything else.

I think the problem is with setting up your FTP server. I assume the directory structure looks something like this:

 ftproot upload download 

It looks like the FTP service automatically registers you with /ftproot/download . That is, the ftp.myserver.com URI maps to /ftproot/download on the local file system. If so, then no amount of messing with the URI will take you anywhere. If the root of the URI maps to the boot directory, you cannot, using the syntax .. , "go up one level and then down."

Can I upload using an FTP client such as Filezilla, or perhaps a Windows FTP command-line tool? If so, what are the steps you are taking to do this? Can you make your code the same way?

+1
source

All Articles