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");
LVL 2
brgdotnetcontractorAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
CarnouConnect With a Mentor Commented:
FTP is one of the trickier tcp/ip standards, because it actually communicates on two different sockets - there's ftp command (port 21), and ftp data (port 20). You'll need to be reading and writing to sockets on both ports to get ftp fully working.
Simply copying a link is a bit of a punt on my side, but check out: http://ondotnet.com/pub/a/dotnet/2004/05/10/ftpdotnet.htm .  O'Reilly stuff is always excellently written, and this explains precisely how a C# FTP client works.  If nothing else, it makes reference to a library written in .NET 1.1 that seems to be freeware and will accomplish what you need.
Good luck!
0
 
brgdotnetcontractorAuthor Commented:
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?
0
 
CarnouCommented:
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!
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
brgdotnetcontractorAuthor Commented:
I have tried FTPWebRequest, but am having the same problem.
0
 
CarnouCommented:
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.
0
 
brgdotnetcontractorAuthor Commented:
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
0
 
CarnouCommented:
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!
0
 
brgdotnetcontractorAuthor Commented:
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?
0
 
CarnouCommented:
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.
0
 
brgdotnetcontractorAuthor Commented:
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.
0
 
brgdotnetcontractorAuthor Commented:
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.
0
All Courses

From novice to tech pro — start learning today.