-1

I'm trying to use the c# library to download a file from an FTP. The code we are using is straight forward.

   static void Main(string[] args)
        {
            Connect(true, true, true);
        }    
            private static void Connect(bool keepAlive, bool useBinary, bool usePassive)
        {
            string RemoteFtpPath = "ftp://ftp.xxxx.ac.uk/incoming/testExtractCSVcoursesContacts.csv";
            const string Username = "anonymous";
            const string Password = "anonymous@xxxx.ac.uk";

            var request = (FtpWebRequest)WebRequest.Create(new Uri(RemoteFtpPath));
            request.Method = WebRequestMethods.Ftp.DownloadFile;
            request.KeepAlive = keepAlive;
            request.UsePassive = usePassive;
            request.UseBinary = useBinary;
            request.Credentials = new NetworkCredential(Username, Password);
            request.Timeout = 30000;

            try
            {
                var response = (FtpWebResponse)request.GetResponse();
                var responseStream = response.GetResponseStream();
                var reader = new StreamReader(responseStream);

                var fileString = reader.ReadToEnd();

                Console.WriteLine(
                    $"Success! keepAlive={keepAlive}, useBinary={useBinary}, usePassive={usePassive} Length={fileString.Length}");

                reader.Close();
                response.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(
                    $"Failed! keepAlive={keepAlive}, useBinary={useBinary}, usePassive={usePassive}, message={e.Message}");
            }
        }
      `

we also tried to set passive = true with identical results. When we run it, using wireshark we are getting : Wireshark log c#

Now we tried the same with Python and it's working just fine:

import urllib.request
data = urllib.request.urlretrieve('path')
print(data)

the wireshark log looks quite different: wireshark log

So tried different things, but not able to sort this out.

Zulu
  • 8,765
  • 9
  • 49
  • 56
Juan Piaggio
  • 190
  • 12
  • 1
    *"not working"* is not a problem description. What goes wrong exactly? Any error/exception? Post a [log file](http://stackoverflow.com/q/9664650/850848), callstack, anything (everything). Your Wireshark log from C# session does not show FTP requests, so we cannot know that the server responds `500` to. Though it's most probably the `OPTS utf8 on`, what the `FtpWebRequest` does not mind getting rejected. – Martin Prikryl Jun 23 '16 at 18:35
  • Yes, the issue was that we were not able to connect.you are right, after doing further research, we found out that the c# library always uses the OPTS on, and it seems this ftp server had a different configuration. After changing the library, everything is sorted now. The FtpWebRequest cannot be configured to work with this kind of severs. Thanks for your help – Juan Piaggio Jun 24 '16 at 10:17

1 Answers1

0

Some ftp servers don't support OPTS UTF8 but still transmit file names in UTF8. (Note that 'OPTs UTF8' is NOT required by the FTP Internationalization Standard, although supporting UTF8 file names is.) The .NET Ftp classes will use the default code page if they don't get an OK response to OPTS UTF8... It's unfortunate that MS didn't provide some way to use UTF8 anyway, since this leaves you unable to transmit international file names to and from otherwise UTF8-compliant servers.

The issue is sorted after using a different library as FtpWebRequest doesn't support it

Juan Piaggio
  • 190
  • 12