我正在尝试从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;
}
Run Code Online (Sandbox Code Playgroud)
我从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.
Run Code Online (Sandbox Code Playgroud)
它停在那里.它返回的字符串数组包含一个带有一些非ascii字符的索引.看起来像一堆垃圾.也许我的ASCII.GetString部分是错的?我不太确定.
提前致谢.
小智 6
我为所有FtpWebRequest的东西编写了一个非常容易使用的包装器库.如果您想查看它,请访问https://gist.github.com/1242616
我在很多生产环境中使用它并没有让我失望.
| 归档时间: |
|
| 查看次数: |
12265 次 |
| 最近记录: |