Why doesn't FTPWebRequest, or WebRequest in ge

2019-05-10 21:27发布

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 address, 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 have tried creating the URI with the no parse option. I have 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 ("/../").

3条回答
甜甜的少女心
2楼-- · 2019-05-10 22:06

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.

查看更多
手持菜刀,她持情操
3楼-- · 2019-05-10 22:13

Have you tried using the @ symbol like so?

Uri target = new Uri(@"ftp://ftpserver.com/../AB00000/incoming/files");
FtpWebRequest request = (FtpWebRequest)WebReqeuest.Create(target);
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-05-10 22:19

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.

查看更多
登录 后发表回答