我試圖從FTP服務器檢索文件列表,但我收到了一些奇怪的非ASCII響應。從C#中的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.
它停在那裏:
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服務器上得到的迴應是這樣的。它返回的字符串數組包含一個帶有一些非ascii字符的索引。看起來像一堆垃圾。也許我的ASCII.GetString
部分是錯誤的?我不太確定。
在此先感謝。
您使用的是什麼版本的.NET Framework? – Kev 2009-11-13 17:17:09
對不起。 3.5。 – Aaron 2009-11-13 17:17:30
只是一個評論...我可以正確下載和上傳文件。列出並返回列表似乎是我不能做的唯一事情。我試過LS,LIST和NLST。由於某種原因LS不被識別,並且LIST和NLST都是這樣做的。 – Aaron 2009-11-13 17:27:40