VB.NET - The requested URI is invalid for this FTP command!

Asked By Dan on 06-Jul-11 12:13 PM
Hello,

I am creating a website that allows users to login and download files from FTP site to their computer.

I have used a CheckBoxList to display a lift of files available for download from an FTP site.

When I go to download the file I get an error message that says:

'The requested URI is invalid for this FTP command.'

It is pointing to the following code:

[CODE]

Dim ftpResponse As FtpWebResponse = ftp.GetResponse()Dim ftpResponse As FtpWebResponse = ftp.GetResponse()

[/CODE]

I have set up the FTP Request as thus:

[CODE]

Dim ftp As FtpWebRequest = DirectCast(FtpWebRequest.Create("ftp://xx.xx.x.xx/In/"), FtpWebRequest)Dim ftp As FtpWebRequest = DirectCast(FtpWebRequest.Create("ftp://xx.xx.x.xx/In/"), FtpWebRequest)

[/CODE]

Is it saying that the URL for the ftp site I am providing is incorrect?

Or does this mean something else?

All help and advice will be greatly appreciated.

Thanks,

Dan
Ravi S replied to Dan on 06-Jul-11 12:42 PM
HI

FtpWebRequest uploadRequest = (FtpWebRequest) Webrequest.Create(uploadUrl + @"/" + fileName) --  where uploadUrl is the place to upload to and fileName is just the name of the file. It turns out that my filename ended up being the location and name of the file and therefore it did not work

refer the links for details
http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/0ba8d3b4-e7da-4130-83bb-db1aa0c974f8
http://forums.asp.net/p/1233240/2242751.aspx
Ravi S replied to Dan on 06-Jul-11 12:42 PM
HI

refer this example

I'm using the following code to do that:

// create a ftp object with the address to communicate

Uri target = new Uri(address);

FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(target);

// specify the type of ftp request to perform

ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;

ftpRequest.UseBinary = true;

// specify the username and password

ftpRequest.Credentials = new NetworkCredential(username, password);

//copy the contents of the file into a byte array

StreamReader sourceStream = new StreamReader(filepath);

Byte[] btFileData = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());

sourceStream.Close();

ftpRequest.ContentLength = btFileData.Length;

try

{

// write the array of data into a requet stream

Stream requestStream = ftpRequest.GetRequestStream();

requestStream.Write(btFileData, 0, btFileData.Length);

requestStream.Close();

}

catch(Exception e)

{

MessageBox.Show(e.Message,"Could not get the request stream");

return;

}

try

{

FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();

}

catch (Exception e)

{

MessageBox.Show(e.Message,"Cannot get the response");

return;

}

 

 refer link also

http://forums.iis.net/p/975537/1236813.aspx#1236813

http://stackoverflow.com/questions/5802516/the-requested-uri-is-invalid-for-this-ftp-command

Brian P replied to Dan on 06-Jul-11 02:28 PM
Your FtpWebRequest is doing a get for a folder name....  Do you really mean to download the entire folder?  You may need to append the file name to the end of the folder name.

You did not list any code where you are setting credentials for the request, so the request will be submitted as an anonymous user.  Does your FTP server support anonymous users?  If so (or if you are in fact passing credentials), check the server to see how the user is defined.  FTP accounts are generally set up to have an initial directory and cannot access any directories or files contained in that folder's parent folders.  You may need to adjust the path to be relative to the account's initial folder instead of using the full path on the server.   For example, if the account (or anonymous user) is set up to go to the /In/ folder, you should use a path of ftp://xx.xx.x.xx instead of ftp://xx.xx.x.xx/In  since part of the path will be handled by the account's login. 

Also, depending on your server, path and file names may be case-sensitive, so watch out for those errors as well.

To debug problems with FTP accounts and paths, try opening Windows explorer (not Internet explorer).  To use anonymous FTP, enter ftp://xx.xx.xx.xx (or ftp://xx.xx.xx.xx/In) in the address bar.  To use an FTP account, enter ftp:userid@ftp://xx.xx.xx.xx in the address bar and then enter the appropriate password when prompted.  Either way, you should eventually see a listing of files on the FTP server.  Once you've verified the credentials and URL, use the same information in your VB code or the code that Ravi posted and you should be able to connect.
Kirtan Patel replied to Dan on 06-Jul-11 10:04 PM
use FtpWebRequest class as below example 
Dim uri As New Uri("ftp://ftp.microsoft.com/ReadMe.txt")
If (uri.Scheme = uri.UriSchemeFtp) Then
      Dim request As FtpWebRequest = FtpWebRequest.Create(uri)
      request.Method = WebRequestMethods.Ftp.DownloadFile
      Dim response As FtpWebResponse = request.GetResponse()
      Dim reader As New StreamReader(response.GetResponseStream())
      Dim tmp As String = reader.ReadToEnd()
      response.Close()
      Me.Label1.Text = tmp
 End If
Thanks
Jitendra Faye replied to Dan on 06-Jul-11 11:53 PM

According to http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1290688&SiteID=1&pageid=0#1291001 -

you need to specify the file name in your uri so that the server knows where to write it.


Also, after the statement GetStream.Close(), you need to use:

Dim response As FtpWebResponse = CType(ftp.GetResponse(), FtpWebResponse);

This will send your request to the FTP Server. You can then use response.Status and response.StatusDescription to see what happened.


Dan replied to Jitendra Faye on 07-Jul-11 04:10 AM
Hello, thanks for your posts.

Surely I only need to specify the file name if im uploading a file.

Whereas I am downloading, so surely I only need to specify the directory path?

Or do I still need to specify the file that is in my CheckBoxList after the URI?

Eg. ftp://xx.xx.x.xx/In/ " & CheckBoxList1.SelectedItem?

Please advise further.

Thanks,

Dan
Dan replied to Ravi S on 07-Jul-11 04:14 AM
Hi Ravi,

I dont want to upload a file, I am aiming to download a file to my system from the site.

Thanks,

Dan
Dan replied to Brian P on 07-Jul-11 06:19 AM
Hi Brian,

Thank you very much for your post.

I am providng user credentials for FTP login.

I have made changes to the path like you suggested.

Do I need to provide an item instance, so it knows what file its looking for?

E.g. ftp://xx.xx.x.xx/" & CheckBoxList1.Text?

Also, if I do need to do that, what type will I be using?

By this I mean that it wont be text will it? would it be CheckBoxList1.Item or CheckBoxList1.Value?

Many Thanks,

Dan