我试图弄清楚如何将此文件上传到C#中的ftp服务器。当它在getResponse()上调用ftpwebrequest时,抛出错误“550-拒绝访问”。我不知道为什么。我可以使用相同的凭据通过Filezilla连接到服务器。

这是我进行连接的代码:

private void UploadFileToFTP(HttpPostedFile file, string server, string user, string pass)
    {
        string uploadUrl = server + file.FileName;
        string uploadFileName = Path.GetFileName(file.FileName);

        Stream streamObj = file.InputStream;
        Byte[] buffer = new Byte[file.ContentLength];
        streamObj.Read(buffer, 0, buffer.Length);
        streamObj.Close();
        streamObj = null;
        try
        {
            SetMethodRequiresCWD();
            FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(uploadUrl);
            //ftp.Method = WebRequestMethods.Ftp.MakeDirectory;
            ftp.Method = WebRequestMethods.Ftp.UploadFile;
            ftp.UsePassive = true;
            ftp.Credentials = new NetworkCredential(user, pass);
            FtpWebResponse CreateForderResponse = (FtpWebResponse)ftp.GetResponse();

            if (CreateForderResponse.StatusCode == FtpStatusCode.PathnameCreated)
            {

                string ftpUrl = string.Format("{0}/{1}", uploadUrl, uploadFileName);

                FtpWebRequest requestObj = FtpWebRequest.Create(ftpUrl) as FtpWebRequest;

                requestObj.KeepAlive = true;
                requestObj.UseBinary = true;

                requestObj.Method = WebRequestMethods.Ftp.UploadFile;
                requestObj.Credentials = new NetworkCredential(user, pass);

                Stream requestStream = requestObj.GetRequestStream();
                requestStream.Write(buffer, 0, buffer.Length);
                requestStream.Flush();
                requestStream.Close();
                requestObj = null;

            }
        }
        catch (WebException e)
        {
            String status = ((FtpWebResponse)e.Response).StatusDescription;
        }
    }

最佳答案

好的,在阅读完这里的评论后,我又做了一些修改。我进入卡巴斯基设置,并禁用了对端口20和21的扫描。文件在那里。现在由于某种原因它空无一人,所以我将对此进行调查或在这里寻求帮助! :)

关于c# - ftpwebrequest.getresponse抛出550访问被拒绝,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33401495/

10-08 23:16