c#ftp只写

时间:2012-09-29 21:55:03

标签: ftpwebrequest writeonly

当我尝试连接到“只写”ftp

时,以下代码将引发异常
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp://...);

request.Credentials = new NetworkCredential("username", "password");

request.Method = WebRequestMethods.Ftp.UploadFile;

Stream ftpstream = request.GetRequestStream();

GetRequestStream()抛出“权限被拒绝”异常,因为框架实现了FTP命令,如下所示: - 打开 - 用户 - pwd

请帮忙吗?

1 个答案:

答案 0 :(得分:0)

查看this主题。

另外,为了避免场景背后的PWD你可以像edtFtpNet那样使用.Net FTP客户端:

        var ftp = new FTPClient();
        ftp.BytesTransferred += new BytesTransferredHandler((o, e) => { Console.WriteLine("Remote Path: " + e.RemotePath + ", Remote File: " + e.RemoteFile + ", Bytes transfered: " + e.ByteCount); });
        ftp.TransferStartedEx += new TransferHandler((o, e) => { Console.WriteLine("FTP Transfer started!"); });
        ftp.TransferCompleteEx += new TransferHandler((o, e) => { Console.WriteLine("FTP Transfer completed!"); });

        try
        {
            ftp.RemoteHost = FTP_HOST;
            ftp.ConnectMode = FTPConnectMode.ACTIVE;
            ftp.Timeout = 120000;
            ftp.Connect();
            ftp.Login(FTP_USER, FTP_PWD);
            ftp.TransferType = FTPTransferType.BINARY;
            ftp.Put(GetBytes("file contents go here!"), Guid.NewGuid().ToString() + ".txt");
        }
        catch (Exception e)
        {
            Console.WriteLine("Error:" + e.ToString());
        }
        finally
        {
            ftp.Quit();
            Console.WriteLine("FTP Quit!");
        }