Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FTP File Upload with HTTP Proxy

Tags:

c#

proxy

ftp

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.

like image 666
Nico Avatar asked Oct 02 '08 07:10

Nico


People also ask

Can you FTP proxy?

The FTP proxy functions as a relay for the File Transfer Protocol to enable you control connections based upon source and destination addresses and user authentication. It can also limit access to certain file transfer commands, such as put and get , based on source or destination addresses and user authentication.

What is an FTP proxy server?

Description. ftp. proxy is a proxy server for a subset of the file tranfer protocol described in RFC 959. It forwards traffic between a client and a server without looking too much if both hosts do real FTP. The FTP server can be either given on the command line or supplied by the client.

What is FTP over HTTP?

FTP over HTTP is a type of HTTP traffic going on between a web browser and a proxy, such as the proxy provided by Web Gateway. In most respects, sending an FTP-over-HTTP request is like sending any other HTTP request. The difference is that the requested resource resides on an FTP server rather than an HTTP server.


4 Answers

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?

like image 172
Alexander Avatar answered Oct 04 '22 03:10

Alexander


most FTP proxies do their thing on the connection, so if you had NO proxy, you do this:

  • server: myftpserver.com
  • user: me
  • password: pwd

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!

like image 32
Nic Wise Avatar answered Oct 04 '22 03:10

Nic Wise


Our Rebex FTP/SSL can use HTTP proxy. It's not free, though...

// initialize FTP client 
Ftp client = new Ftp();

// setup proxy details  
client.Proxy.ProxyType = FtpProxyType.HttpConnect;
client.Proxy.Host = proxyHostname;
client.Proxy.Port = proxyPort;

// add proxy username and password when needed 
client.Proxy.UserName = proxyUsername;
client.Proxy.Password = proxyPassword;

// connect, login 
client.Connect(hostname, port);
client.Login(username, password);

// do some work 
// ... 

// disconnect 
client.Disconnect();
like image 23
Martin Vobr Avatar answered Oct 04 '22 05:10

Martin Vobr


As of the .NET framework 4.8, the FtpWebRequest still cannot upload files over HTTP proxy.

If the specified proxy is an HTTP proxy, only the DownloadFile, ListDirectory, and ListDirectoryDetails commands are supported.

And it probably never will as FtpWebRequest is now deprecated. So you need to use a 3rd party FTP library.


For example with WinSCP .NET assembly, you can use:

// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "example.com",
    UserName = "user",
    Password = "mypassword",
};

// Configure proxy
sessionOptions.AddRawSettings("ProxyMethod", "3");
sessionOptions.AddRawSettings("ProxyHost", "proxy");

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    // Upload file
    string localFilePath = @"C:\path\file.txt";
    string pathUpload = "/file.txt";
    session.PutFiles(localFilePath, pathUpload).Check();
}

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)

like image 21
Martin Prikryl Avatar answered Oct 04 '22 04:10

Martin Prikryl