如何解决FTP 550错误"访问是拒绝"因为上传时连接不良

时间:2013-10-26 14:15:48

标签: c# .net ftp ftpwebrequest

如何解决FTP 550错误“访问是拒绝”,因为上传时连接错误。 我确定我的服务器已经给予了许可,因为有时候我可以毫无问题地上传。 我读过一些帖子说发生这种情况是因为服务器连接速度慢但我想知道是否有人可以解决这个问题

这是我的代码:

public bool FTPUploadFunct(string uploadto2, string newskinlocation2)
    {
        bool FTPUploadFunct = true;
        MethodInvoker methodInvokerDelegate = delegate()
        { toolStripStatusLabel1.Text = "Uploading...."; };
        this.Invoke(methodInvokerDelegate);
        try
        {
            //delete old file
            FtpWebRequest requestFileDelete = (FtpWebRequest)WebRequest.Create(uploadto2);
            requestFileDelete.Credentials = new NetworkCredential("FTPUser20", "1234");
            requestFileDelete.Method = WebRequestMethods.Ftp.DeleteFile;

            FtpWebResponse responseFileDelete = (FtpWebResponse)requestFileDelete.GetResponse();

            //upload new file
            FtpWebRequest requestFTPUploader = (FtpWebRequest)WebRequest.Create(uploadto2);
            requestFTPUploader.Credentials = new NetworkCredential("FTPUser20", "1234");
            requestFTPUploader.Method = WebRequestMethods.Ftp.UploadFile;

            FileInfo fileInfo = new FileInfo(newskinlocation2);
            FileStream fileStream = fileInfo.OpenRead();

            int bufferLength = 2048;
            byte[] buffer = new byte[bufferLength];

            Stream uploadStream = requestFTPUploader.GetRequestStream();
            int contentLength = fileStream.Read(buffer, 0, bufferLength);

            while (contentLength != 0)
            {
                backgroundWorker1.ReportProgress((int)(contentLength / fileInfo.Length));
                uploadStream.Write(buffer, 0, contentLength);
                contentLength = fileStream.Read(buffer, 0, bufferLength);
            }

            uploadStream.Close();
            fileStream.Close();
            backgroundWorker1.ReportProgress(100);

            requestFTPUploader = null;
        }
        catch (WebException ex)
        {
            FTPUploadFunct = false;
            String status = ((FtpWebResponse)ex.Response).StatusDescription;
            MessageBox.Show(status + "error while in FTPUploadFunc");
            errorNumber = (int)((FtpWebResponse)ex.Response).StatusCode;
            if (errorNumber == 550)
            {
                MessageBox.Show("550" + "error while in FTPUploadFunc2");
            };
        }
        return FTPUploadFunct;
    } 
很多时候它一直停留在

requestFileDelete.Method = WebRequestMethods.Ftp.DeleteFile;

但有时在

requestFTPUploader.Method = WebRequestMethods.Ftp.UploadFile;

1 个答案:

答案 0 :(得分:0)

最后我发现90%的成功率。 并在上传新文件前等待约3.5秒

Cursor = Cursors.WaitCursor;
System.Threading.Thread.Sleep(3500);
Cursor = Cursors.Default;