1

当我尝试连接到“只写”ftp 时,以下代码将引发异常

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp://...);

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

request.Method = WebRequestMethods.Ftp.UploadFile;

Stream ftpstream = request.GetRequestStream();

GetRequestStream() 抛出“权限被拒绝”异常,因为框架实现 FTP 命令如下: - open - user - pwd

请帮忙?

4

1 回答 1

0

看看这个线程。

此外,为了避免幕后的 PWD,您可以使用 .Net FTP 客户端,例如edtFtpNet,如下所示:

        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!");
        }
于 2012-11-22T11:05:07.533 回答