8

FtpWebRequest我已经使用C# 中的类实现了上传、下载、删除等功能。这是相当直截了当的。

我现在需要做的是支持发送任意 FTP 命令,例如

quote SITE LRECL=132 RECFM=FB
or 
quote SYST

这是直接来自我们的示例配置app.config

<!-- The following commands will be executed before any uploads occur -->
<extraCommands>
     <command>quote SITE LRECL=132 RECFM=FB</command>
</extraCommands>

我仍在研究如何使用FtpWebRequest. 接下来我可能会尝试WebClient上课。任何人都可以更快地指出我正确的方向吗?谢谢!

更新:我得出了同样的结论,从 .NET Framework 3.5 开始FtpWebRequest,除了WebRequestMethods.Ftp.*. 我将尝试其他一些帖子推荐的第三方应用程序。谢谢您的帮助!

4

4 回答 4

9

我不认为它可以用FtpWebRequest......指定FTP命令的唯一方法是通过Method属性,并且文档指出:

请注意,WebRequestMethods.Ftp类中定义的字符串是该属性唯一受支持的选项Method。将该Method属性设置为任何其他值将导致ArgumentException异常。

SITE 和 SYST 不在预定义选项中,所以我猜你被卡住了......

不要浪费时间去尝试这个WebClient类,它给你的灵活性甚至比FtpWebRequest.

但是,有很多第三方 FTP 实现,开源的或商业的,我很确定其中一些可以处理自定义命令......

于 2010-02-23T21:17:43.477 回答
7

正如Thomas Levesque在他的回答中所说,这FtpWebRequest对您没有帮助。您可以使用一些第三方解决方案或以下基于简化的代码,这些代码是我从用 Visual Basic 编写的答案中重构的:TcpClient

public static void SendFtpCommand()
{
    var serverName = "[FTP_SERVER_NAME]";
    var port = 21;
    var userName = "[FTP_USER_NAME]";
    var password = "[FTP_PASSWORD]"
    var command = "SITE CHMOD 755 [FTP_FILE_PATH]";

    var tcpClient = new TcpClient();
    try
    {
        tcpClient.Connect(serverName, port);
        Flush(tcpClient);

        var response = TransmitCommand(tcpClient, "user " + userName);
        if (response.IndexOf("331", StringComparison.OrdinalIgnoreCase) < 0)
            throw new Exception(string.Format("Error \"{0}\" while sending user name \"{1}\".", response, userName));

        response = TransmitCommand(tcpClient, "pass " + password);
        if (response.IndexOf("230", StringComparison.OrdinalIgnoreCase) < 0)
            throw new Exception(string.Format("Error \"{0}\" while sending password.", response));

        response = TransmitCommand(tcpClient, command);
        if (response.IndexOf("200", StringComparison.OrdinalIgnoreCase) < 0)
            throw new Exception(string.Format("Error \"{0}\" while sending command \"{1}\".", response, command));
    }
    finally
    {
        if (tcpClient.Connected)
            tcpClient.Close();
    }
}

private static string TransmitCommand(TcpClient tcpClient, string cmd)
{
    var networkStream = tcpClient.GetStream();
    if (!networkStream.CanWrite || !networkStream.CanRead)
        return string.Empty;

    var sendBytes = Encoding.ASCII.GetBytes(cmd + "\r\n");
    networkStream.Write(sendBytes, 0, sendBytes.Length);

    var streamReader = new StreamReader(networkStream);
    return streamReader.ReadLine();
}

private static string Flush(TcpClient tcpClient)
{
    try
    {
        var networkStream = tcpClient.GetStream();
        if (!networkStream.CanWrite || !networkStream.CanRead)
            return string.Empty;

        var receiveBytes = new byte[tcpClient.ReceiveBufferSize];
        networkStream.ReadTimeout = 10000;
        networkStream.Read(receiveBytes, 0, tcpClient.ReceiveBufferSize);

        return Encoding.ASCII.GetString(receiveBytes);
    }
    catch
    {
        // Ignore all irrelevant exceptions
    }

    return string.Empty;
}

通过 FTP 时,您可以期待以下流程:

220 (vsFTPd 2.2.2)
user [FTP_USER_NAME]
331 Please specify the password.
pass [FTP_PASSWORD]
230 Login successful.
SITE CHMOD 755 [FTP_FILE_PATH]
200 SITE CHMOD command ok.
于 2015-09-29T15:19:11.040 回答
3

你可以试试我们的Rebex FTP 组件

// create client and connect 
Ftp client = new Ftp();
client.Connect("ftp.example.org");
client.Login("username", "password");

// send SITE command
// note that QUOTE and SITE are ommited. QUOTE is command line ftp syntax only.
client.Site("LRECL=132 RECFM=FB");

// send SYST command
client.SendCommand("SYST");
FtpResponse response = client.ReadResponse();
if (response.Group != 2) 
  ; // handle error 

// disconnect 
client.Disconnect();
于 2010-02-24T17:05:44.413 回答
-5

利用sendCommand("SITE LRECL=242 BLKSIZE=0 RECFM=FB");

于 2010-06-10T10:29:46.173 回答