C# FTP 550 error

2019-02-25 06:26发布

问题:

I'm trying to programatically download a file in C# via FTP, here is the relevant code (obviously with fake credntials and URI):

try
{
    var request = FtpWebRequest.Create("ftp://ftp.mydomain.com/folder/file.zip");
    request.Credentials = new NetworkCredential("username", "password");

    using (var response = request.GetResponse())
    {
         ...
    }
}
catch (WebException we)
{
    ...
}

The exception is thrown at request.GetResponse(), and the error code is 550. The issue is not with the credentials or the URI, as they work just fine in IE and the file downloads successfully there. What am I missing? Is there some other kind of credential object I should be using? Is there a property on the request object I'm not setting? Any help would be appreciated.

回答1:

Turns out the FTP root isn't necessarily the same as the URL root. Perhaps I'm mixing up terminology, so let me explain: in my case, connecting to ftp.mydomain.com already starts at /folder, so my URL needed to just be ftp://ftp.mydomain.com/file.zip. IE8 knows how to eliminate the redundant /folder part in the original path while the FtpRequest class does not, which is why it worked in IE8 but not in the C# code.



回答2:

Put another slash at the beginning of the local path part (after hostname:port):

var request = FtpWebRequest.Create("ftp://ftp.mydomain.com//folder/file.zip");

This has worked for me.



回答3:

Here is what I use, I bet the .Method is the main thing you are missing

        request = (FtpWebRequest)FtpWebRequest.Create(address);
        request.Credentials = new NetworkCredential("username", "password");
        request.UsePassive = true;
        request.UseBinary = true;
        request.Proxy = null;

        request.Method = WebRequestMethods.Ftp.DownloadFile;
        FtpWebResponse dataResponce = (FtpWebResponse)request.GetResponse();


回答4:

You have not set the Method.

 request.Method = WebRequestMethods.Ftp.DownloadFile;


回答5:

The Connection to my FTP-Server shows different behaviors, depending from where I try to connect. In my local Network it works with just one slash after the domain-part, e.g. ftp://test.test.com/folder/myfile.txt

But on a remote machine I have to use two slashes like ftp://test.test.com//folder/myfile.txt

The two-slashes-approach works in both cases.



回答6:

I recently had this problem and after much testing discovered that some module between here and there did not like a mixed case URI and so my resolution was to use .ToLower() on the URI



标签: c# ftp