Is there a way to upload a file to a FTP server when behind an HTTP proxy ?
It seems that uploading a file is not supported behind an HTTP Proxy using .Net Webclient. (http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.proxy.aspx).
If there is no workaround ? If not, do you know a good and free FTP library I can use ?
Edit: Unfortunately, I don't have any FTP proxy to connect to.
Id don't really see the connection between a http proxy and uploading to an ftp server. If you use the http proxy class thats for accessing http resources trough a http proxy. ftp is another protocol and the ftp proxies use a different protocol.
As of the .NET framework 4.7.2, the
FtpWebRequest
still cannot upload files over HTTP proxy.So either you need to implement the upload yourself, or use a 3rd party FTP library.
For example with WinSCP .NET assembly, you can use:
For the options for tje
SessionOptions.AddRawSettings
, see raw settings.Easier is to have WinSCP GUI generate C# FTP code template for you.
Note that WinSCP .NET assembly is not a native .NET library. It's rather a thin .NET wrapper over a console application.
(I'm the author of WinSCP)
Our Rebex FTP/SSL can use HTTP proxy. It's not free, though...
The standard way of uploading content to an ftp:// URL via an HTTP proxy would be using an HTTP PUT request. An HTTP proxy acts as an HTTP<->FTP gateway when dealing with ftp:// URLs, speaking HTTP to the requesting client and FTP to the requested FTP server.
At least the Squid HTTP Proxy supports PUT to ftp:// URLs, not sure what other proxies do.
The more common way is by abusing the CONNECT method to esablish tunnels over the proxy. But this is often not allowed due to security implications of allowing bidirectional tunnels over the proxy.
most FTP proxies do their thing on the connection, so if you had NO proxy, you do this:
using an FTP proxy, you do:
and it just works it out from there. I'm using this RIGHT THIS SECOND (trying to debug something) thru a squid proxy.
... but as you dont have an FTP proxy....
Do you have a SOCKS proxy? That might work, but I dont know if .NET can do it. Otherwise, to be honest, I think you are stuck! FTP is an "odd" protocol, when compared to HTTP, as it has a control channel (port 21) and a data channel (or more than one, on a random port), so going via proxies is.... fun to say the least!
In active FTP mode, the server initiates a data connection to the client. If the client is behind an HTTP proxy, this obviously won't work. In passive FTP mode it is the client who initiates both the initial and the data connections. Since HTTP proxies can tunnel arbitrary outgoing TCP connections (using the CONNECT method), it should be possible to access an FTP server in passive mode via an HTTP proxy.
The
FtpWebRequest
seems to support passive mode. However, I don't understand why file download and directory listings are supported, whereas file upload, which also uses the same data connection, is not.Have you confirmed that
FtpWebRequest
configured for passive mode does not work via an HTTP proxy through which directory listings/file download work just fine?