4

I'm using the FtpWebRequest and FtpWebResponse objects in the System.Net namespace to issue a LIST command. The problem I'm having is that the FTP server I'm connecting to does not have the OPTS command implemented.

Is there a way of preventing FtpWebRequest from issuing the OPTS command?

skaffman
  • 398,947
  • 96
  • 818
  • 769
GP.
  • 1,293
  • 5
  • 14
  • 20

3 Answers3

5

I'm afraid you can't do that... According to Reflector, it seems to be hard-coded in an internal class method (FtpControlStream.BuildCommandsList), so you can't override it. However it shouldn't be an issue, the request should continue even if the OPTS command fails (see the code for FtpControlStream.PipelineInstruction in Reflector)

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • 1
    Actually you can stop it but it's a bit of a hack (short version: use the async response method, inject your own WriteCallbackDelegate into the CommandStream, modify the Commands list in your delegate, hand control back to the default delegate). Comment back if you want more elaboration (I'll probably write it up as a post at some point, but that'd poke me to do it faster). – Matt Mitchell Jun 29 '12 at 07:22
  • @MattMitchell I'm stuck on this too. Would you care to elaborate on this? – SupremeDud Jul 28 '12 at 04:03
  • @SupremeDud - Sure, I'll write it up now and post back in a couple of hours – Matt Mitchell Jul 29 '12 at 10:16
  • @SupremeDud - Posted a first-draft. Let me know if that helps (code at the end). http://mattmitchell.com.au/how-to-change-the-commands-ftpwebrequest-sends/ – Matt Mitchell Jul 29 '12 at 16:54
3

It's not the most elegant workaround, but you can modify the commands the FtpWebRequest sends by:

  1. Use the async methods (Begin/End)
  2. Inject your own WriteCallbackDelegate into the underlying FtpWebRequest CommandStream.
  3. Modify the CommandStream's Commands list in your injected elegate
  4. Pass control back to the default callback delegate

I've written up some draft detail on how to do this, but feel free to comment there/here or email me if anyone sees this and need more details.

Matt Mitchell
  • 40,943
  • 35
  • 118
  • 185
3

Actually, it is an issue, because the file names may not be encoded correctly... 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.

jw.
  • 31
  • 1
  • This can be solved with our Rebex FTP component http://www.rebex.net/ftp-ssl.net/. It tries to autodetect encoding using OPTS command, however you can override it by setting the Ftp.Encoding property to Encoding.UTF8. – Martin Vobr Jun 28 '12 at 15:48