FTP File Upload with HTTP Proxy

2019-01-13 21:14发布

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.

标签: c# proxy ftp
13条回答
Juvenile、少年°
2楼-- · 2019-01-13 21:55

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.

查看更多
小情绪 Triste *
3楼-- · 2019-01-13 21:56

As of the .NET framework 4.7.2, 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.

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:

// 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)

查看更多
Evening l夕情丶
4楼-- · 2019-01-13 22:03

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();
查看更多
▲ chillily
5楼-- · 2019-01-13 22:03

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.

查看更多
Anthone
6楼-- · 2019-01-13 22:06

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:

  • server: ftpproxy.mydomain.com
  • user: me@myftpserver.com
  • password: pwd

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!

查看更多
别忘想泡老子
7楼-- · 2019-01-13 22:07

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?

查看更多
登录 后发表回答