Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ftpwebrequest.getresponse is throwing 550 access denied

Tags:

c#

.net

ftp

I am trying to figure out how to get this file uploaded to my ftp server in C#. When it calls getResponse() on ftpwebrequest it is throwing an error that says "550 - access denied". I cannot figure out why. I can connect to the server with Filezilla just fine using the same credentials.

Here is my code that does the connection:

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;
        }
    }
like image 303
dmikester1 Avatar asked Oct 31 '22 16:10

dmikester1


1 Answers

OK, I tinkered around with this some more after reading through the comments here. I went into my Kaspersky settings and disabled scanning of port 20 and 21. Boom! File is there. Now it is coming across empty for some reason, so I will investigate that or come back for some help here! :)

like image 59
dmikester1 Avatar answered Nov 15 '22 05:11

dmikester1