Thursday, June 18, 2009

FTP File Transfer - Not Logged In Exception

When I found this error, I did a google search on it and found that most of the queries have been replied as something to do with the XML data.
But then, what the heck, I don't have any XML data in my code. Very soon however, I found the answer. I was trying to set the mode for the FTP Data Transfer even before logging into the FTP Server. Changed that and everything is set right. :)

Before:


FtpClient ftp = null;
try
{
ftp = new FtpClient();


ftp.openServer(FTP_SERVER);


if (filename.endsWith(".txt") || filename.endsWith(".csv"))
{
ftp.ascii();
}
else
{
ftp.binary();
}

ftp.login(FTP_USER, FTP_PASSWORD);
if (REMOTE_DIR != null) ftp.cd("REMOTE_DIR);

// Rest Of the Code Here

}
catch (Exception e)
{
throw e;
}


After:


FtpClient ftp = null;
try
{
ftp = new FtpClient();
ftp.openServer(FTP_SERVER);
ftp.login(FTP_USER, FTP_PASSWORD);
if (REMOTE_DIR != null) ftp.cd("REMOTE_DIR);

if (filename.endsWith(".txt") || filename.endsWith(".csv"))
{
ftp.ascii();
}
else
{
ftp.binary();
}
// Rest Of the Code Here

}
catch (Exception e)
{
throw e;
}


Very simple indeed, but it did put me in a fix for a while and I found out one more thing for sure.. GOOGLE SEARCH might not be the best thing at all times. :)

No comments :