Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The requested URI is invalid for this FTP command

Tags:

c#

.net

I have added the following code in my C#.net application in visual studio 2010

 WebRequest request = WebRequest.Create("ftp://myftp.com");
            request.Method = WebRequestMethods.Ftp.MakeDirectory;
            request.Credentials = new NetworkCredential("myusername", "12344");
            using (var resp = (FtpWebResponse)request.GetResponse())
            {
                Console.WriteLine(resp.StatusCode);
            }

But I am getting following error.

The requested URI is invalid for this FTP command.

Please suggest solution. Thanks

like image 929
DotnetSparrow Avatar asked Apr 27 '11 10:04

DotnetSparrow


3 Answers

If you just want to check file information you should try changing the request.Method to WebRequestMethods.Ftp.ListDirectoryDetails instead:

WebRequest request = WebRequest.Create("ftp://myftp.com");
        request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
        request.Credentials = new NetworkCredential("myusername", "12344");
        using (var resp = (FtpWebResponse)request.GetResponse())
        {
            Console.WriteLine(resp.StatusCode);
        }
like image 70
skeryl Avatar answered Nov 05 '22 15:11

skeryl


Uri should be ftp:// + IP or ftpServerName + / Folder_to_create_Name

So, see follow code:

 WebRequest request = WebRequest.Create("ftp://myftp.com/FOLDER_TO_CREATE/");
 request.Method = WebRequestMethods.Ftp.MakeDirectory;
 request.Credentials = new NetworkCredential("myusername", "12344");

 using (var resp = (FtpWebResponse)request.GetResponse())
 {
       Console.WriteLine(resp.StatusCode);
 }
like image 22
Natali Avatar answered Nov 05 '22 17:11

Natali


You must specify a sub-directory you wish to create; you can't create the root, which is what it appears you are trying to do. Hence the error.

like image 3
Andrew Barber Avatar answered Nov 05 '22 16:11

Andrew Barber