OPTS command on FtpWebRequest/FtpWebResponse
-
20-09-2019 - |
Question
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?
Solution
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)
OTHER TIPS
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.
It's not the most elegant workaround, but you can modify the commands the FtpWebRequest
sends by:
- Use the async methods (Begin/End)
- Inject your own
WriteCallbackDelegate
into the underlyingFtpWebRequest
CommandStream
. - Modify the
CommandStream
'sCommands
list in your injected elegate - 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.