C# 如何通过StreamSocket(winRT)发送FTP命令

C# 如何通过StreamSocket(winRT)发送FTP命令,c#,windows-runtime,windows-phone-8.1,stream-socket-client,C#,Windows Runtime,Windows Phone 8.1,Stream Socket Client,我正在尝试从WindowsPhone8.1连接到我的filezilla ftp服务器 我现在有这个代码: StreamSocket s = new StreamSocket(); await s.ConnectAsync(new HostName("192.168.254.53"), "21"); DataWriter writer = new DataWriter(s.OutputStream); byte[] data = GetBytes(string.Format("{0}\r\n",

我正在尝试从WindowsPhone8.1连接到我的filezilla ftp服务器

我现在有这个代码:

StreamSocket s = new StreamSocket();
await s.ConnectAsync(new HostName("192.168.254.53"), "21");
DataWriter writer = new DataWriter(s.OutputStream);
byte[] data = GetBytes(string.Format("{0}\r\n", "USER test"));
writer.WriteBytes(data);
await writer.StoreAsync();
await writer.FlushAsync();
...
static byte[] GetBytes(string str)
{
    byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
    return bytes;
}
ConnectAsync工作正常:

(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> Connected on port 21, sending welcome message...
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> 220-FileZilla Server version 0.9.49 beta
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> 220-written by Tim Kosse (tim.kosse@filezilla-project.org)
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> 220 Please visit https://filezilla-project.org/
但对于用户命令,这是服务器接收的内容:

(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> U
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> 500 Syntax error, command unrecognized.
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> S
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> 500 Syntax error, command unrecognized.
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> E
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> 500 Syntax error, command unrecognized.
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> R
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)> 500 Syntax error, command unrecognized.
(000013)03/03/2015 17:14:13 - (not logged in) (192.168.254.53)>  
我是否正确地写入了OutputStream?

我现在使用的是:

byte[] data = Encoding.Unicode.GetBytes("USER ippon\r");
在调试期间,我注意到:

Encoding.Unicode.GetBytes函数在每隔一个字节后添加一个0字节。这就是它不起作用的原因! 我现在的代码:

StreamSocket s = new StreamSocket();
await s.ConnectAsync(new HostName("192.168.254.53"), "21");
byte[] data = Encoding.Unicode.GetBytes("USER ippon\r");
data = data.Where(val => val != 0).ToArray();
await s.OutputStream.WriteAsync(data.AsBuffer());
await s.OutputStream.FlushAsync();

不确定,但是FTP命令不是8位ASCII吗?net字符是16位的。你能使用
编码
类吗?我尝试了
GetBytes(string.Format(“{0}\r”,“USER test”)
Encoding.UTF8.GetBytes(“USER ippon”)
Encoding.bigendiaunicode.GetBytes(“USER ippon”)
Encoding.Unicode.GetBytes(“USER ippon”)
但它们一直给我相同的结果…我将代码更改为
byte[]data=Encoding.Unicode.GetBytes(“用户ippon\r”);
并且我注意到函数在命令的每个字母之间添加一个字节到0。这就是为什么服务器将每个字母解释为一个命令…所以我只需删除所有0字节,它就可以工作了!