Link to home
Start Free TrialLog in
Avatar of brgdotnet
brgdotnetFlag for United States of America

asked on

using C# to access an ftp server ?

I am trying to open an ftp connection using C# to read all files off of the ftp server.
I have logged in manually from dos using ftp so my credentials are correct. When the
last line of code below is executed, I get this error message:

"The requested URI is invalid for this FTP command"

Is there an expert who can help me with this?

// C# code below

WebClient request = new WebClient();

request.Credentials = new NetworkCredential("us","klop");

Stream myStream = new request.OpenRead("ftp://10.333.12.333");
ASKER CERTIFIED SOLUTION
Avatar of Carnou
Carnou
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brgdotnet

ASKER

Hi Expert, I appreciate the post. It is not helpful because they are using a third party source code to perform the ftp methods. Plus this articel was posted in 2004 so I am thinking that the current .net framework has better plumbing for ftp.

Possibly someone else can help me out has done something similar to my approach?
Ok, you're right, there are much more modern versions.  So now, a question for you - why are you using WebClient?  The more modern examples with .NET support for FTP would use FtpWebRequest (see, e.g. http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/0ba8d3b4-e7da-4130-83bb-db1aa0c974f8).  By using a simple WebRequest, I was assuming you wanted to reinvent the wheel and deal with all the underlying communications yourself.
I'm still punting by giving you a link, but maybe you'll like this one better.
Hope this helps!
I have tried FTPWebRequest, but am having the same problem.
Have you tried, instead of
Stream myStream = new request.OpenRead("ftp://10.333.12.333");
doing
Stream myStream = new request.OpenRead("ftp://10.333.12.333//FILENAME.EXT");
where FILENAME.EXT is the name of the file you want to get/put?  Based on the previous msdn link I sent you, that seems to be the solution.
Other than that, I can't access your ftp site from my computer, so can't do more detailed testing.  I don't even think the command-line ftp can handle addresses like the ones you have, since it includes number greater than 255.
Thank you so much for helping me Sir. Yes I tried that earlier with not success. I tried it again and it does not work. Stepping through the code and looking at the request properties ContentType it says the following:"request.PreAuthenticate' threw an exception of type'System.NotSupportedException"

Other properties show that as well such as the UseDefaultCredentials property
I'm not certain, but I think the exception you're seeing while stepping through is a red herring - there are a lot of things that throw exceptions to the debugger that are not really errors, so much as something being visible but not yet ready.  ContentType would make sense as one of them - the debugger is looking for it, but doesn't know what its value is until you submit the request or until it's returned.
Other than that, and a bunch of Googling, I don't currently have any suggestions, and am away from the desk for the weekend.  Hopefully, someone else can help you out.
Good luck!
Reallly cool responses on your part. I am wondering if I can find any free ftp sites on the world wide web to experiment with?
Hi, brqdotnet,
I didn't want you to feel I have totally abandoned your question, but I haven't taken time to create my own ftp client to give you a solution.
If you are still working on this, I would like to ask you to be very specific about the process you've gone through in testing.  I know you say you've tried appending the filename earlier but not gotten success, but I'd like more concrete information as to what you have tried as your URL.
The reason I ask this is because I see your original question mentions you were looking for a listing of an FTP directory, as opposed to trying to upload or download a file.  The address syntax for that won't work.  My understanding is that uploading and downloading via a specified URL are shortcuts, but that the procedure is different for getting a directory listing.  http://www.codeproject.com/KB/IP/ftplib.aspx has a c# library that should do all this for you.  It's .NET 1.1, but it's a freely available library.  It can at least give you a baseline.
If you would like to post your existing code, I can try playing with it till I get it to work.
In the meantime, some questions to consider in the interest of having better debugging ideas:
  • You've tested via a command-line utility.  Just to be sure: have you successfully gotten a return from the "dir" command when you log in?
  • On the command-line utility, have you made sure it checks your password correctly?  (ie, enter an invalid password)
  • On the command line utility, have you tested the root directory you log in to, and a subdirectory?
  • In your code, what are the exact line/lines of the URL you have tested?  Have any of them worked?
  • Am I correct in assuming that you have not gotten get, put, or dir to work via your code?
  • http://www.ftp-sites.org/ lists all sorts of anonymous ftp sites.  You can pick any one of them, and try doing your tests without credentials, to see if the issue is in the credentials or in the rest of the code.
Sorry I have more questions than answers, but hopefully, this will get us in the right direction.
Dear Carnou,

Management has asked that this project be postponed and I am now working on other things. I will not be able to test your suggestions until later. In the meantime I am going to assign you the points. Only later to return and leave comments about your suggestion. God bless you for spending so much time on this. to help me, Have a great day.
I will return later to leave comments on my success. I will have to do this on my own time as management has postponed the project.