Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2006

Tip: Looking for answers? Try searching our database.

FtpWebRequest Connection Issue

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
BobbyLupo - 26 Oct 2006 13:24 GMT
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);
     }      
   }
Bryan Phillips - 30 Oct 2006 01:37 GMT
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

> 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.
[quoted text clipped - 51 lines]
>       }
>     }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.