-1

I am trying to read a CSV file from ftp server using c# but problem is it does not allow me to read and throwing this error

The remote server returned an error: (530) Not logged in.

I have spent lot of time on researching this issue unfortunately I could not find the solution yet. Any help would be highly appreciated.

Here is the code:

StringBuilder result = new StringBuilder();
FtpWebRequest reqFTP;
try
{
    String ftpserver = "ftp://domain.com/StatusChanges_20120922_043057.csv";

    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpserver));
    reqFTP.UsePassive = false;
    reqFTP.UseBinary = true;
    reqFTP.Credentials = new NetworkCredential("test@domain.com", "password");
    reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
    reqFTP.Proxy = GlobalProxySelection.GetEmptyWebProxy();
    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

    StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
    string line = "";

    while (reader.Peek() > -1)
    {
        line = reader.ReadLine();
        Console.WriteLine(line);//**********HTML was wrote out here*************
    }

    if (result.ToString().LastIndexOf('\n') >= 0)
        result.Remove(result.ToString().LastIndexOf('\n'), 1);
    reader.Close();
    response.Close();

    return result.ToString();
}
catch (Exception ex)
{
}
Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
Asp.Net Dev
  • 135
  • 1
  • 3
  • 10

4 Answers4

2

What you need to do is get it connecting properly with an external FTP client such which I assume you already have done. Once you have another FTP client connecting properly you can double check that all the credentials and settings match. Once you have done this and it is still not working you should analyse what the external FTP client and your program are sending to the server using something like Wireshark and look at the difference in the request headers. It may be that there is a simple setting you are missing.

Sean Dawson
  • 5,587
  • 2
  • 27
  • 34
1

Not sure, but I don't know that Peek() is the ideal choice to detect whether there is more data to be read. Peek() can return -1 if the stream doesn't support seeking as you're attempting here. Think the preferred method is to use ReadLine in a loop until it returns null...

David W
  • 10,062
  • 34
  • 60
0
  1. Check if your file location is exactly right - you've got a long file name there, so triple check it's correct.

  2. Make sure your username / password for the FTP are correct. If they are, make sure they have the correct permissions on the FTP Server in order to access that file.

  3. Make sure your FTP server has not hit the maximum number of connections before you connect to it.

Chris Dixon
  • 9,147
  • 5
  • 36
  • 68
0

Make the single slash "/" to double slash "//" in the FTP server URL.

Dont use StreamReader. Just use stream writer to write the file byte[] array.