0

I have to upload files to a ftp site. i am able transfer file to that ftp folder using WINSCP

Now I am trying the same using c# with below code

FtpWebRequest ftpClient = (FtpWebRequest)FtpWebRequest.Create(
"ftp://ftps-xxxxx.xxx.xx.com:xxx/folder/text.txt");
         ftpClient.Credentials = new System.Net.NetworkCredential("username", "password");
          ftpClient.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
          ftpClient.UseBinary = true;
          ftpClient.KeepAlive = true;
          System.IO.FileInfo fi = new System.IO.FileInfo(@"C:\d\text.txt");
          ftpClient.ContentLength = fi.Length;
          byte[] buffer = new byte[4097];
          int bytes = 0;
          int total_bytes = (int)fi.Length;
          System.IO.FileStream fs = fi.OpenRead();
          ftpClient.KeepAlive = false;
          ftpClient.Timeout = -1;
          System.IO.Stream rs = ftpClient.GetRequestStream();
          while (total_bytes > 0)
          {
             bytes = fs.Read(buffer, 0, buffer.Length);
             rs.Write(buffer, 0, bytes);
             total_bytes = total_bytes - bytes;
           }
          fs.Close();
          rs.Close();
          FtpWebResponse uploadResponse = (FtpWebResponse)ftpClient.GetResponse();
          string value = uploadResponse.StatusDescription;
          uploadResponse.Close();

when it reach System.IO.Stream rs = ftpClient.GetRequestStream(); below error is occuring

The remote server returned an error: (500) Syntax error, command unrecognized.

Sachu
  • 7,555
  • 7
  • 55
  • 94
  • Using a traditional client with given URL and credentials works? Why `KeepAlive = true` then `false` a little later? – Arnaud F. Mar 27 '17 at 10:50
  • You can easily simplify you transfer request as given in MSDN : https://msdn.microsoft.com/en-us/library/ms229715(v=vs.110).aspx – Arnaud F. Mar 27 '17 at 10:50
  • @ArnaudF. just tried with `false` also forget to remove the `true` from beginning..Also yes using WINSCP client for the same url and credentials works – Sachu Mar 27 '17 at 10:54
  • [Enable logging](http://stackoverflow.com/q/9664650/850848) and show us the log + Show us WinSCP log. – Martin Prikryl Mar 27 '17 at 11:49
  • Also you claim you have problems with TLS/SSL, yet I do not see anything in your code that you enable it. – Martin Prikryl Mar 27 '17 at 11:50
  • @MartinPrikryl thanks one of the issue was not enabling it. – Sachu Mar 28 '17 at 02:58

1 Answers1

0

I think you don't need to reinvent the wheel, simply transfer like this:

        using (System.Net.WebClient c = new System.Net.WebClient())
        {
            c.Credentials = new System.Net.NetworkCredential("username", "password");
            c.UploadFile("ftp://...", @"C:\d\test.txt");
        }
Arnaud F.
  • 8,252
  • 11
  • 53
  • 102