Home All Groups Group Topic Archive Search About

FtpWebRequest Connection Issue

Author
26 Oct 2006 12:24 PM
BobbyLupo
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);
      }      
    }

Author
30 Oct 2006 12:37 AM
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




Show quote
"BobbyLupo" <BobbyL***@discussions.microsoft.com> wrote in message
news:40500687-65CA-4190-8FB2-6728CCF4FD91@microsoft.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.
> 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);
>       }
>     }

AddThis Social Bookmark Button