在C#中从FTP服务器检索文件列表

我正在尝试从FTP服务器检索文件列表,但我得到了一些奇怪的非ASCII响应。

这是我正在使用的代码:

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; } 

我从FTP服务器得到的响应如下:

 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. 

它停在那里。 它返回的字符串数组包含一个带有一些非ascii字符的索引。 看起来像一堆垃圾。 也许我的ASCII.GetString部分是错误的? 我不太确定。

提前致谢。

我为所有FtpWebRequest的东西编写了一个非常容易使用的包装器库。 如果您想查看它,请访问https://gist.github.com/1242616

我在很多生产环境中使用它并没有让我失望。

对于它的价值,System.Net命名空间具有以.Net 2.0开头的FtpWebRequest和FtpWebResponse类。

这是我用过的一些代码,用于将服务器的文件写入本地文件:

 ... 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(); ... 
 public Socket BuildDataConn(Socket FirstSocket) { string ret = ""; string RemoteIP = ""; int RemotePort = 0; SendCommand("PASV"); int id = 0; id = strMsg.LastIndexOf(')'); if (id != 0) { string tmp = strMsg.Substring(strMsg.LastIndexOf('(') + 1, id - strMsg.LastIndexOf('(') - 1); string[] bByte = tmp.Split(','); RemotePort = int.Parse(bByte[4]) * 256 + Byte.Parse(bByte[5]); RemoteIP = bByte[0]+"." + bByte[1] + "." + bByte[2] + "." + bByte[3]; } Socket NewConn = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint RemoteIPPort = new IPEndPoint(IPAddress.Parse(RemoteIP), RemotePort); try { NewConn.Connect(RemoteIPPort); } catch { throw new IOException("unable to Connect !"); } return NewConn; } 

您需要获取第二个套接字,然后使用套接字接收响应数据,而不是使用cSocket接收响应数据。 第二个套接字将响应发回信息。 并确保在发送命令LIST之前更改了FTP工作目录。

 public List GetFileNames() { if (!bConnected) { Open(); } List retObj = new List(); Socket dataSock = BuildDataConn(mySocket); SendCommand("NLST"); string dataSockMsg = ""; buffer = new byte[BLOCK_SIZE]; while (true) { int iBytes = dataSock.Receive(buffer, buffer.Length, 0); dataSockMsg += System.Text.Encoding.ASCII.GetString(buffer, 0, iBytes); if (iBytes < buffer.Length) { break; } } string[] message = dataSockMsg.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); dataSock.Close(); foreach (string obj in message) { retObj.Add(obj); } return retObj; }