tags:

views:

405

answers:

2

I'm trying to retrieve a list of files from an FTP server, but I'm getting some weird non-ASCII responses.

Here is the code that I am using:

 public string[] getFileList(string mask)
 {
   if(!logined)
   {
     login();
   }
   Socket cSocket = createDataSocket();
   this.getSslDataStream(cSocket);
   sendCommand("PASV");
   sendCommand("LIST " + "*"+mask);
   stream2.AuthenticateAsClient(remoteHost,
      null,
      System.Security.Authentication.SslProtocols.Ssl3 |
      System.Security.Authentication.SslProtocols.Tls,
      true);
   if(!(retValue == 150 || retValue == 125))
   {
     throw new IOException(reply.Substring(4));
   }
   StringBuilder mes = new StringBuilder();       
   while(true)
   {
     int bytes = cSocket.Receive(buffer, buffer.Length, 0);
     mes.Append(ASCII.GetString(buffer, 0, bytes));
     if(bytes < buffer.Length)
     {
       break;
     }
   }
   string[] seperator = {"\r\n"};
   string[] mess = mes.ToString().Split(seperator, StringSplitOptions.RemoveEmptyEntries);
   cSocket.Close();
   readReply();
   if(retValue != 226)
   {
     throw new IOException(reply.Substring(4));
   }
   return mess;
 }

The response I get from the FTP server is this:

WRITE:PASV

READ:227 Entering Passive Mode (10,0,2,24,5,119)`

WRITE:LIST *.dat

READ:150 Opening ASCII mode data connection for /bin/ls.

READ:226 Transfer complete.

It stops there. The string array that it returns contains one index with some non-ascii characters. Looks like a bunch of garbage. Perhaps my ASCII.GetString part is wrong? I'm not quite sure.

Thanks in advance.

+2  A: 

For what it's worth, the System.Net namespace has the FtpWebRequest and FtpWebResponse classes beginning in .Net 2.0.

Here's some code I've used that writes the server's files to a local file:

...
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(address);
ftpRequest.Credentials = new NetworkCredential(username, password);
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
ftpRequest.KeepAlive = false;

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

sr = new StreamReader(ftpResponse.GetResponseStream());
sw = new StreamWriter(new FileStream(fileName, FileMode.Create));

sw.WriteLine(sr.ReadToEnd());
sw.Close();

ftpResponse.Close();
sr.Close();
...
Austin Salonen
I know. I'm using some code that was written 2 years ago, but works fine for uploading and downloading in other programs. I'd like to use the same code if possible.
Aaron
I wouldn't rewrite all of it -- just listing the files.
Austin Salonen
Ok. I tried it, but my URI is giving an unknown format exception. It should just be my ftp server name right?
Aaron
It needs to be in the form of `ftp://url.com`
Austin Salonen
A: 

I wrote a pretty easy to use wrapper library for all the FtpWebRequest stuff. If you care to check it out, it's here http://bit.ly/3IL4kW

I use it in a lot of production environments and it hasn't failed me yet.

Jason Kostempski