2

I created an FTP, and I want to read some data using C# code. When the FTP has no username/password access, everything works perfectly. But when I put username and password, I get The remote server returned an error: (530) Not logged in.

I tried all the questions on Stack Overflow and the internet like using .Normalize(), and using @username, but I keep getting that error.

this is my code:

foreach (string fileNameInFTP in directories)
                {
                    //                string fileNameInFTP2 = Path.GetFileNameWithoutExtension(fileNameInFTP);
                    if ((!haveWeAlreadyParsedThisFile(fileNameInFTP)) && (fileNameInFTP.Contains("CustsExport")) && (!fileNameInFTP.EndsWith("Empty.xml")) && (!fileNameInFTP.Contains("DelCustsExport")))
                    {
                        string file = FTPAddress + "/" + fileNameInFTP;
                        Console.WriteLine(file);
                        List<Customer> customersList =
                        (
                            from e in XDocument.Load(file).Root.Elements("cust")
                            select new Customer
                            {
                                MemeberID = (int)e.Attribute("memberid"),
                                CustomerID = (int)e.Attribute("custid"),
                                FirstName = (string)e.Attribute("fname"),
                                LastName = (string)e.Attribute("lname"),
                                ShowsNumber = (int)e.Attribute("count_noshow"),
                                VisitNumber = (int)e.Attribute("count_resos"),
                                Cancellation = (int)e.Attribute("count_cancel"),
                                MobileNumber = (string)e.Element("phone").Attribute("phonenumber")
                                /*Projects =
                                (
                                    from p in e.Elements("projects").Elements("project")
                                    select new Project
                                    {
                                        ProjectCode = (string)p.Element("code"),
                                        ProjectBudget = (int)p.Element("budget")
                                    }).ToArray()*/
                            }).ToList();

Note:

I am able to access the FTP because the `directories` variable is the list of the files in the FTP, and when I debug the code, I can see that it **has** the files, but the exception accurs in this line:
                    List<Customer> customersList =
                    (
                        from e in XDocument.Load(file).Root.Elements("cust")
                        select new Customer
                        {
                            MemeberID = (int)e.Attribute("memberid"),
                            CustomerID = (int)e.Attribute("custid"),
                            FirstName = (string)e.Attribute("fname"),
                            LastName = (string)e.Attribute("lname"),
                            ShowsNumber = (int)e.Attribute("count_noshow"),
                            VisitNumber = (int)e.Attribute("count_resos"),
                            Cancellation = (int)e.Attribute("count_cancel"),
                            MobileNumber = (string)e.Element("phone").Attribute("phonenumber")
                            /*Projects =
                            (
                                from p in e.Elements("projects").Elements("project")
                                select new Project
                                {
                                    ProjectCode = (string)p.Element("code"),
                                    ProjectBudget = (int)p.Element("budget")
                                }).ToArray()*/
                        }).ToList();

In other words: I am able to read the names of the files, but not the content of them.

jthulhu
  • 7,223
  • 2
  • 16
  • 33
Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253
  • @DJKRAZE why are you talking about the `upload` ?, my problem is nothing about upload. – Marco Dinatsoli Dec 23 '14 at 19:43
  • Sound like the FTP server is returning 530. Where inn your code are you connecting to the server? – Aaron Dec 23 '14 at 19:54
  • The problem is that `XDocument.Load(file)` is trying to make a *new connection* to the FTP server, and it doesn't have the necessary credentials. So it throws an exception. – Jim Mischel Dec 23 '14 at 21:00
  • @JimMischel so what is the solution please? – Marco Dinatsoli Dec 23 '14 at 22:18
  • I'd consider creating a `FtpWebRequest` to get the file, and when you get the response, do `XmlDocument.Load(response.GetResponseStream())`. Something like that. I don't have working code for you, but that approach should work. – Jim Mischel Dec 23 '14 at 22:35
  • You might try the FTP url with the username and password in the URL. That is, if you're trying to download `ftp://ftp.foo.com/path/filename`, you can encode the username and password like this: `ftp://username:password@ftp.foo.com/path/filename`. That might do it for you. See Section 3.2 of [RFC 1738](ftp://ftp.funet.fi/pub/doc/rfc/rfc1738.txt) for more info. You would pass that URL to `XDocument.Load`. Note, however, that the password is sent in the clear. – Jim Mischel Dec 24 '14 at 03:06
  • @JimMischel Your solution works well, could you post an answer to accept it please? Many thanks – Marco Dinatsoli Dec 24 '14 at 10:19

1 Answers1

0

You can supply the user name and password on the FTP URL. For example, if you want to download /path/filename from ftp://ftp.foo.com, you can encode the user name and password like this:

ftp://username:password@ftp.foo.com/path/filename

You should be able to construct such a URL for the file you're trying to download. Then pass that URL to XDocument.Load.

Note, however, that the password is sent in the clear (i.e. no encryption, etc.).

See Section 3.2 of RFC 1738 for more info.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351