views:

931

answers:

4

I am trying to automate some upload/download tasks from an ftp web server. When I connect to the server through client, or through Firefox even, in order to get to my directory, I have to specify a path like this:

ftp://ftpserver.com/../AB00000/incoming/files

If I try to access this:

ftp://ftpserver.com/AB00000/incoming/files

The server throws an error that the directory does not exist. So, the problem:

I am trying to create an FTPWebRequest with the first ftp addresss, but it always parses out the "/../" part and then my server says the path doesn't exist.

I've tried these:

    Uri target =

new Uri("ftp://ftpserver.com/../AB00000/incoming/files"); FtpWebRequest request = (FtpWebRequest)WebReqeuest.Create(target);

and

string target = "ftp://ftpserver.com/../AB00000/incoming/files";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);

In the first bit, the path is already incorrect when the Uri object is instantiated, in the second bit, it's after the WebRequest.Create method. Any ideas what's going on?

EDIT:

Additionally, since I posted this, I've tried creating the URI with the no parse option. I've also tried something like this:

string ftpserver = "ftp://ftpserver.com/../";
string path = "12345/01/01/file.toupload";

Uri = new Uri(ftpserver, path, true);

And it always parses out the root part ("/../").

A: 

Have you tried using the @ symbol like so?

Uri target = new Uri(@"ftp://ftpserver.com/../AB00000/incoming/files");
FtpWebRequest request = (FtpWebRequest)WebReqeuest.Create(target);
BFree
Tried that, it does the same thing.
scottm
No characters in that string could be confused for escape characters, so making it a vertbatim string won't have any effect.
Richard Szalay
+8  A: 

Try escaping the .. with something like:

Uri target = new Uri("ftp://ftpserver.com/%2E%2E/AB00000/incoming/files");

That works according to this blog which I found in this discussion.

Bearddo
+1 very nice - this does preserve the "/../"
Andrew Hare
That was it. Wow. The server chroots access to an ftpusers/user folder for each user, I have to login as an account that has access to all other user folders which is why the /../ has to be included.
scottm
+3  A: 

Not really sure about it, but it may be for security reasons, since allowing "/../" URIs would potentially let people navigate freely on any server's file system.

Also, the official URI RFC states that when resolving an URI one of the steps performed is actually the removal of "/../" segments, so it's not a problem in the C# library but it's regular URI behavior.

Raibaz
A: 

This FTP File Upload in C# .Net user-defined function helped me out.