We are facing on random basis for same user name & pwd, The remote server returned an error: (530) Not logged in.
Please suggest , if we find any issue in below code :
public bool FTPFile()
{
try
{
if (IsDirectoryExistAtRemote() == false)
CreateRemoteDirectory();
string URI = "ftp://" + FTPServer + "/" + DirectoryAtFTPServer + "/" + FileNameAtFTPServer;
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(URI);
try
{
// Create a Uri instance with the specified URI string.
// If the URI is not correctly formed, the Uri constructor
// will throw an exception.
ManualResetEvent waitObject;
string fileName = FileToFTP;
FtpState state = new FtpState();
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpRequest.Credentials = new NetworkCredential(FTPUsername, FTPPassword);
ftpRequest.Proxy = new WebProxy();
ftpRequest.ConnectionGroupName = "GroupName";
ftpRequest.KeepAlive = false;
// Store the request in the object that we pass into the
// asynchronous operations.
state.Request = ftpRequest;
state.FileName = fileName;
// Get the event to wait on.
waitObject = state.OperationComplete;
// Asynchronously get the stream for the file contents.
ftpRequest.BeginGetRequestStream(
new AsyncCallback(EndGetStreamCallback),
state
);
// Block the current thread until all operations are complete.
waitObject.WaitOne();
// The operations either completed or threw an exception.
if (state.OperationException != null)
{
FTPOperationStatus = ReportEnum.ExportStatus.Failed;
throw state.OperationException;
}
else
{
FTPOperationStatus = ReportEnum.ExportStatus.Finished;
return true;
}
}
finally
{
ftpRequest.ServicePoint.CloseConnectionGroup("GroupName");
}
}
catch (Exception ex)
{
FTPFailureDescription = ex.Message;
//FTPFailureDescription = ((FtpWebResponse)we.Response).StatusDescription;
FTPOperationStatus = ReportEnum.ExportStatus.Failed;
return false;
}
}
private bool IsDirectoryExistAtRemote()
{
FtpWebResponse response= null;
FtpWebRequest ftpRequest = null;
try
private
{
string URI = "ftp://" + FTPServer + "/" + DirectoryAtFTPServer;
ftpRequest = (FtpWebRequest)WebRequest.Create(URI);
ftpRequest.ConnectionGroupName = "GroupName";
ftpRequest.KeepAlive = false;
ftpRequest.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;
ftpRequest.Credentials = new NetworkCredential(FTPUsername, FTPPassword);
ftpRequest.Proxy = new WebProxy();
ftpRequest.UseBinary = true;
response = ftpRequest.GetResponse() as FtpWebResponse;
return true;
}
catch (WebException ex)
{
response = (FtpWebResponse)ex.Response;
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
return false;
else
return true;
}
finally
{
ftpRequest.ServicePoint.CloseConnectionGroup("GroupName");
if (response!= null)
response.Close();
}
}
private bool CreateRemoteDirectory()
{
try
{
string URI = "ftp://" + FTPServer + "/" + DirectoryAtFTPServer;
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(URI);
ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
ftpRequest.Credentials = new NetworkCredential(FTPUsername, FTPPassword);
ftpRequest.Proxy = new WebProxy();
FtpWebResponse response = ftpRequest.GetResponse() as FtpWebResponse;
response.Close();
if (response.StatusCode == FtpStatusCode.PathnameCreated)
return true;
else
return false;
}
catch (WebException ex)
{
throw new Exception("Directory could not be created at remote location");
}
}