C# 使用 HTTP 代理上传 FTP 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/161200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:13:33  来源:igfitidea点击:

FTP File Upload with HTTP Proxy

提问by Nico

Is there a way to upload a file to a FTP server when behind an HTTP proxy ?

有没有办法在 HTTP 代理后面将文件上传到 FTP 服务器?

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

似乎在使用 .Net Webclient 的 HTTP 代理后面不支持上传文件。(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 ?

如果没有解决方法?如果没有,您知道我可以使用的一个好的免费 FTP 库吗?

Edit: Unfortunately, I don't have any FTP proxy to connect to.

编辑:不幸的是,我没有任何 FTP 代理可以连接。

回答by Vasil

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.

我没有真正看到 http 代理和上传到 ftp 服务器之间的连接。如果您使用 http 代理类通过 http 代理访问 http 资源。ftp 是另一种协议,而 ftp 代理使用不同的协议。

回答by Alexander

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.

在主动 FTP 模式下,服务器向客户端发起数据连接。如果客户端位于 HTTP 代理之后,这显然不起作用。在被动 FTP 模式下,客户端启动初始连接和数据连接。由于 HTTP 代理可以建立任意传出 TCP 连接的隧道(使用 CONNECT 方法),因此应该可以通过 HTTP 代理以被动模式访问 FTP 服务器。

The FtpWebRequestseems 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.

FtpWebRequest似乎支持被动模式。但是,我不明白为什么支持文件下载和目录列表,而不支持同样使用相同数据连接的文件上传。

Have you confirmed that FtpWebRequestconfigured for passive mode does not work via an HTTP proxy through which directory listings/file download work just fine?

您是否确认FtpWebRequest配置为被动模式不能通过 HTTP 代理工作,目录列表/文件下载通过该代理工作正常吗?

回答by Nic Wise

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

大多数 FTP 代理在连接上做他们的事情,所以如果你没有代理,你可以这样做:

  • server: myftpserver.com
  • user: me
  • password: pwd
  • 服务器:myftpserver.com
  • 用户:我
  • 密码:pwd

using an FTP proxy, you do:

使用 FTP 代理,您可以:

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....

...但由于您没有 FTP 代理....

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!

你有SOCKS代理吗?这可能有效,但我不知道 .NET 是否可以做到。否则,老实说,我认为你被卡住了!与 HTTP 相比,FTP 是一种“奇数”协议,因为它有一个控制通道(端口 21)和一个数据通道(或多个,在一个随机端口上),所以通过代理......至少说吧!

回答by VVS

If there's a way for you to upload a file via FTP withoutC# then it should also be possible in C#. Does uploading via browser or an FTP client work?

如果有一种方法可以让您在没有C# 的情况下通过 FTP 上传文件,那么在 C# 中也应该是可能的。通过浏览器或 FTP 客户端上传是否有效?

The one FTP library I like the most is .NET FTP Client library.

我最喜欢的一个 FTP 库是.NET FTP Client library

回答by Darron

As Alexander says, HTTP proxies can proxy arbitrary traffic. What you need is an FTP Client that has support for using a HTTP Proxy. Alexander is also correct that this would only work in passive mode.

正如亚历山大所说,HTTP 代理可以代理任意流量。您需要的是支持使用 HTTP 代理的 FTP 客户端。亚历山大也是正确的,这只能在被动模式下工作。

My employer sells such an FTP client, but it is a enterprise level tool that only comes as part of a very large system.

我的雇主出售这样的 FTP 客户端,但它是企业级工具,仅作为非常大系统的一部分提供。

I'm certain that there are others available that would better fit your needs.

我敢肯定还有其他可以更好地满足您的需求。

回答by Alexander

One solution is to try Mono's implementation of FtpWebRequest. I had a look at its source code and it appears it'll be easy to modify so that all connections (control and data) are tunneled via an HTTP proxy.

一种解决方案是尝试 Mono 的 FtpWebRequest 实现。我查看了它的源代码,它似乎很容易修改,以便所有连接(控制和数据)都通过 HTTP 代理进行隧道传输。

You establish a TCP connection to your HTTP proxy instead of the actual FTP server. Then you send CONNECT myserver:21 HTTP/1.0followed by two CRLFs (CRLF = \r\n). If the proxy needs authentication, you need to use HTTP/1.1 and also send a proxy authentication header with the credentials. Then you need to read the first line of the response. If it starts with "HTTP/1.0 200 " or "HTTP/1.1 200 ", then you (the rest of the code) can continue using the connection as though it's connected directly to the FTP server.

您建立到 HTTP 代理的 TCP 连接,而不是实际的 FTP 服务器。然后发送CONNECT myserver:21 HTTP/1.0后跟两个 CRLF (CRLF = \r\n)。如果代理需要身份验证,则需要使用 HTTP/1.1 并发送带有凭据的代理身份验证标头。然后你需要阅读响应的第一行。如果它以“ HTTP/1.0 200 ”或“ HTTP/1.1 200 ”开头,那么您(其余代码)可以继续使用该连接,就像它直接连接到 FTP 服务器一样。

回答by Martin Vobr

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

我们的Rebex FTP/SSL可以使用 HTTP 代理。不过不是免费的。。。

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

回答by dvn

I've just had the same problem.

我刚刚遇到了同样的问题。

My primary goal was to upload a file to an ftp. And I didn't care if my traffic would go through proxy or not.

我的主要目标是将文件上传到 ftp。而且我不在乎我的流量是否会通过代理。

So I've just set FTPWebRequest.Proxy property to null right after FTPWebRequest.Create(uri).

所以我刚刚在 FTPWebRequest.Create(uri) 之后将 FTPWebRequest.Proxy 属性设置为 null。

And it worked. Yes, I know this solution is not the greatest one. And more of that, I don't get why did it work. But the goal is complete, anyway.

它奏效了。是的,我知道这个解决方案不是最好的解决方案。还有更多,我不明白为什么它起作用了。但无论如何,目标已经完成。

回答by Henrik Nordstr?m

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.

通过 HTTP 代理将内容上传到 ftp:// URL 的标准方法是使用 HTTP PUT 请求。HTTP 代理在处理 ftp:// URL 时充当 HTTP<->FTP 网关,将 HTTP 与请求的客户端对话,并将 FTP 与请求的 FTP 服务器对话。

At least the Squid HTTP Proxy supports PUT to ftp:// URLs, not sure what other proxies do.

至少 Squid HTTP 代理支持 PUT 到 ftp:// URL,不确定其他代理会做什么。

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.

更常见的方法是滥用 CONNECT 方法在代理上建立隧道。但由于允许通过代理的双向隧道的安全隐患,这通常是不允许的。

回答by miconico

Hi I had the same issue - the resolution was to create the proxy object and derive the defaultcredentials - this should be fine provided your application is been run with a network account -

嗨,我遇到了同样的问题-解决方案是创建代理对象并派生默认凭据-如果您的应用程序是使用网络帐户运行的,这应该没问题-

FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));

System.Net.WebProxy proxy = System.Net.WebProxy.GetDefaultProxy();
proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

// set the ftpWebRequest proxy
reqFTP.Proxy = proxy;

This resolved the issue for me.

这为我解决了这个问题。