2

I'm trying to upload files to an FTPS fileshare. But I can't get the code to work. When the code below starts the program just hangs, without any errors. Eventually there will come a timeout error that says system error. I've tried many things, also library's etc. Does anyone have experience with an FTPS upload? This code works with a normal FTP I've tried.

var ftpServerIP = "Ftp.company1.company2.nl:990";
var ftpUserID = "Username";
var ftpPassword = "Password";

FileInfo fileInf = new FileInfo(@"D:\testfile.txt");
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;

// Create FtpWebRequest object from the Uri provided
reqFTP =
    (FtpWebRequest)FtpWebRequest.Create(
        new Uri("ftp://" + ftpServerIP + "/IDEE_MOX/" + fileInf.Name));
reqFTP.EnableSsl = true;

// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;

reqFTP.ContentLength = fileInf.Length;

int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;

FileStream fs = fileInf.OpenRead();

try
{
    Stream strm = reqFTP.GetRequestStream();
    contentLen = fs.Read(buff, 0, buffLength);
    while (contentLen != 0)
    {
        strm.Write(buff, 0, contentLen);
        contentLen = fs.Read(buff, 0, buffLength);
    }
    strm.Close();
    fs.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "Upload Error");
}

Does anyone knows what's wrong with this code and why its freezing? I'm kind of stuck here.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Wouter
  • 33
  • 1
  • 8
  • on which line does it freeze? – BugFinder Jan 16 '17 at 13:38
  • @BugFinder It runs Stream strm = reqFTP.GetRequestStream(); and then it freezes – Wouter Jan 16 '17 at 13:40
  • 1
    You are using port 990. Is the server listening for ftp on this port? Do you want to use FTP or FTPS? Port 990 is for ftp TSL/SSL. Since it is a well known port number (under1028) it may be blocked by a firewall or virus checker. Port 990 is for control and data is sent on different port number. You should use a sniffer like wireshark or fiddler to help develope the code. – jdweng Jan 16 '17 at 13:47
  • @jdweng Yes the server is a port 990 FTPS (SSL DIRECT). I can connect to the server with filezilla without any problems. So what you are trying to say is that there is something on my pc (firewall or virus checker etc) blocking this port? And that's way it's freezing? – Wouter Jan 16 '17 at 14:00
  • I think the data is on a different port number and you are only using one port. Use a sniffer with filezilla and compare results with your application – jdweng Jan 16 '17 at 15:26

1 Answers1

3

The .NET framework (FtpWebRequest) does not support an implicit TLS/SSL. Only explicit, see:
Does .NET FtpWebRequest Support both Implicit (FTPS) and explicit (FTPES)?

So, you cannot connect to the port 990 (implicit TLS/SSL).


Though as your FTP server most likely supports the explicit TLS/SSL too, just use it. You already set the EnableSsl (what is explicit TLS/SSL). So just connect to the default port 21 and it's done:

var ftpServerIP = "Ftp.company1.company2.nl"; 

(no explicit port needs to be specified, as the 21 is the default)


In rare situation that your server does not support the explicit TLS/SSL, you need to use a 3rd party FTP client library. Some are mentioned in the question linked above. My WinSCP .NET assembly supports the implicit FTPS too. As you have already made the connection working with WinSCP GUI, using the implicit TLS/SSL, you can have a code template generated.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Thank you for your answer but it isn't that simple. When i remove the :990 from the url, and go over to ftp i will get an error that I'm "Unable to connect to the remote server". I have read the aricle you provided but i do not want to use third third-party dll. But it looks that the use of use third third-party dll's is my only option. – Wouter Jan 16 '17 at 14:25
  • Can you connect using a standalone FTP client to the server using an explicit TLS/SSL? E.g WinSCP. – Martin Prikryl Jan 16 '17 at 14:28
  • I've tried connection with WinSCP with an explicit TLS/SSL and it failed. Implicit worked fine – Wouter Jan 16 '17 at 15:08
  • OK, so then your FTP server indeed does not support explicit FTPS. Or something (firewall/NAT/etc) blocks the 21 port. What was the error with WinSCP? – Martin Prikryl Jan 16 '17 at 15:09
  • This was the error "No connection could be made because the target machine actively refused it. Connection failed." – Wouter Jan 16 '17 at 15:16
  • OK, so as I've said, either the server listens on port 990 only, or something blocks the port 21. – Martin Prikryl Jan 16 '17 at 15:17