FtpWebRequest Connection Issue

G

Guest

Hello. I am trying to use FtpWebRequest to send a file. Everything was
initially working fine until the ftp server had a load balancer installed.
Now when I connect and try to send a file, I get the following exception:

{System.Net.WebException} The data connection was made from an address that
is different than the address to which the FTP connection was made.

Does anyone know how to handle this? Here is the code I am using to send the
file:

public bool SendFtpFile(FtpParamaters paramaters)
{
try
{
bool result = false;

// Read local file
byte[] fileBytes = GetBytes(paramaters.LocalFile);

// Initialize Ftp Request
Uri target = new Uri(paramaters.FtpHost + paramaters.FtpHostPath +
paramaters.LocalFile.Name);
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(target);
ftp.Credentials = new
NetworkCredential(paramaters.LoginName,paramaters.Password);
ftp.KeepAlive = true;
ftp.UseBinary = false;
ftp.UsePassive = false;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
ftp.Timeout = paramaters.FtpTimeout; ;
ftp.CachePolicy = new
System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);

// load file into ftp request
ftp.ContentLength = fileBytes.Length;

// *** THE FOLLOWING LINE IS WHERE THE EXCEPTION IS THROWN *** //
Stream requestStream = ftp.GetRequestStream();
requestStream.Write(fileBytes, 0, fileBytes.Length);
requestStream.Close();


// Make the Ftp Request
FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
if (response.StatusDescription.Trim() == "226 Transfer complete.")
result = true;

response.Close();
return result;

}
catch (Exception ex)
{
throw new FtpException("Error sending file", ex);
}
}
 
B

Bryan Phillips

Try using ftp.UsePassive = true;

If you still get a similar error, you will have to programmatically
determine to which IP address you are getting directed to and try to
connect to that address instead.

Bryan Phillips
MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top