使用HTTP代理进行FTP文件上载

时间:2021-07-09 03:43:18

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代理连接。

13 个解决方案

#1


9  

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

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

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?

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

#2


5  

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

using an FTP proxy, you do:

使用FTP代理,您可以:

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

它只是从那里开始工作。通过squid代理,我正在使用这个正确的第二个(试图调试一些东西)。

... 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)和一个数据通道(或多个,在随机端口上),所以通过代理进入是有趣的。至少说!

#3


5  

Our Rebex FTP/SSL can 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();

#4


3  

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.0 followed 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服务器一样。

#5


3  

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

从.NET framework 4.7.2开始,FtpWebRequest仍无法通过HTTP代理上传文件。

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

如果指定的代理是HTTP代理,则仅支持DownloadFile,ListDirectory和ListDirectoryDe​​tails命令。

So either you need to implement the upload yourself, or use a 3rd party FTP library.

因此,您需要自己实施上传,或使用第三方FTP库。


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

例如,使用WinSCP .NET程序集,您可以使用:

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

有关tje SessionOptions.AddRawSettings的选项,请参阅原始设置。

Easier is to have WinSCP GUI generate C# FTP code template for you.

更容易让WinSCP GUI为您生成C#FTP代码模板。

Note that WinSCP .NET assembly is not a native .NET library. It's rather a thin .NET wrapper over a console application.

请注意,WinSCP .NET程序集不是本机.NET库。它相当于控制台应用程序上的瘦.NET包装器。

(I'm the author of WinSCP)

(我是WinSCP的作者)

#6


2  

If there's a way for you to upload a file via FTP without C# 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客户端库。

#7


2  

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.

我确信还有其他可用的东西可以更好地满足您的需求。

#8


2  

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请求。在处理ftp:// URL,向请求客户端说HTTP和向请求的FTP服务器发送FTP时,HTTP代理充当HTTP < - > 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方法在代理上建立隧道。但由于允许双向隧道通过代理的安全隐患,通常不允许这样做。

#9


2  

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 -

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

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.

这解决了我的问题。

#10


0  

Damn these unfree applications and components!!! Here is my open source C# code that can uploads file to FTP via HTTP proxy.

该死的应用程序和组件!这是我的开源C#代码,可以通过HTTP代理将文件上传到FTP。

        public bool UploadFile(string localFilePath, string remoteDirectory)
    {
        var fileName = Path.GetFileName(localFilePath);
        string content;
        using (var reader = new StreamReader(localFilePath))
            content = reader.ReadToEnd();

        var proxyAuthB64Str = Convert.ToBase64String(Encoding.ASCII.GetBytes(_proxyUserName + ":" + _proxyPassword));
        var sendStr = "PUT ftp://" + _ftpLogin + ":" + _ftpPassword
            + "@" + _ftpHost + remoteDirectory + fileName + " HTTP/1.1\n"
            + "Host: " + _ftpHost + "\n"
            + "User-Agent: Mozilla/4.0 (compatible; Eradicator; dotNetClient)\n" + "Proxy-Authorization: Basic " + proxyAuthB64Str + "\n"
            + "Content-Type: application/octet-stream\n"
            + "Content-Length: " + content.Length + "\n"
            + "Connection: close\n\n" + content;

        var sendBytes = Encoding.ASCII.GetBytes(sendStr);

        using (var proxySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
        {
            proxySocket.Connect(_proxyHost, _proxyPort);
            if (!proxySocket.Connected)
                throw new SocketException();
            proxySocket.Send(sendBytes);

            const int recvSize = 65536;
            var recvBytes = new byte[recvSize];
            proxySocket.Receive(recvBytes, recvSize, SocketFlags.Partial);

            var responseFirstLine = new string(Encoding.ASCII.GetChars(recvBytes)).Split("\n".ToCharArray()).Take(1).ElementAt(0);
            var httpResponseCode = Regex.Replace(responseFirstLine, @"HTTP/1\.\d (\d+) (\w+)", "$1");
            var httpResponseDescription = Regex.Replace(responseFirstLine, @"HTTP/1\.\d (\d+) (\w+)", "$2");
            return httpResponseCode.StartsWith("2");
        }
        return false;
    }

#11


-1  

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.

它奏效了。是的,我知道这个解决方案不是最好的解决方案。更重要的是,我不明白为什么它有效。但无论如何,目标是完整的。

#12


-1  

I'm not sure if all HTTP proxies work in the same way, but I managed to cheat ours by simply creating an HTTP request to access resource on URI ftp://user:pass@your.server.com/path.

我不确定所有HTTP代理是否都以相同的方式工作,但我设法通过简单地创建HTTP请求来访问URI ftp:// user:pass@your.server.com/path上的资源来欺骗我们。

Sadly, to create an instance of HttpWebRequest you should use WebRequest.Create. And if you do that you can't create an HTTP request for ftp:// schema.

遗憾的是,要创建HttpWebRequest的实例,您应该使用WebRequest.Create。如果这样做,则无法为ftp:// schema创建HTTP请求。

So I used a bit of reflection to invoke a non-public constructor which does that:

所以我使用了一些反射来调用非公共构造函数来执行此操作:

var ctor = typeof(HttpWebRequest).GetConstructor(
    BindingFlags.NonPublic | BindingFlags.Instance, 
    null, 
    new Type[] { typeof(Uri), typeof(ServicePoint) }, 
    null);
var req = (WebRequest)ctor.Invoke(new object[] { new Uri("ftp://user:pass@host/test.txt"), null });
req.Proxy = new WebProxy("myproxy", 8080);
req.Method = WebRequestMethods.Http.Put;

using (var inStream = req.GetRequestStream())
{
    var buffer = Encoding.ASCII.GetBytes("test upload");
    inStream.Write(buffer, 0, buffer.Length);
}

using (req.GetResponse())
{
}

You can also use other methods like "DELETE" for other tasks.

您还可以使用其他方法(如“DELETE”)执行其他任务。

In my case, it worked like a charm.

就我而言,它就像一个魅力。

#13


-5  

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代理使用不同的协议。

#1


9  

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

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

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?

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

#2


5  

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

using an FTP proxy, you do:

使用FTP代理,您可以:

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

它只是从那里开始工作。通过squid代理,我正在使用这个正确的第二个(试图调试一些东西)。

... 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)和一个数据通道(或多个,在随机端口上),所以通过代理进入是有趣的。至少说!

#3


5  

Our Rebex FTP/SSL can 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();

#4


3  

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.0 followed 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服务器一样。

#5


3  

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

从.NET framework 4.7.2开始,FtpWebRequest仍无法通过HTTP代理上传文件。

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

如果指定的代理是HTTP代理,则仅支持DownloadFile,ListDirectory和ListDirectoryDe​​tails命令。

So either you need to implement the upload yourself, or use a 3rd party FTP library.

因此,您需要自己实施上传,或使用第三方FTP库。


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

例如,使用WinSCP .NET程序集,您可以使用:

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

有关tje SessionOptions.AddRawSettings的选项,请参阅原始设置。

Easier is to have WinSCP GUI generate C# FTP code template for you.

更容易让WinSCP GUI为您生成C#FTP代码模板。

Note that WinSCP .NET assembly is not a native .NET library. It's rather a thin .NET wrapper over a console application.

请注意,WinSCP .NET程序集不是本机.NET库。它相当于控制台应用程序上的瘦.NET包装器。

(I'm the author of WinSCP)

(我是WinSCP的作者)

#6


2  

If there's a way for you to upload a file via FTP without C# 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客户端库。

#7


2  

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.

我确信还有其他可用的东西可以更好地满足您的需求。

#8


2  

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请求。在处理ftp:// URL,向请求客户端说HTTP和向请求的FTP服务器发送FTP时,HTTP代理充当HTTP < - > 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方法在代理上建立隧道。但由于允许双向隧道通过代理的安全隐患,通常不允许这样做。

#9


2  

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 -

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

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.

这解决了我的问题。

#10


0  

Damn these unfree applications and components!!! Here is my open source C# code that can uploads file to FTP via HTTP proxy.

该死的应用程序和组件!这是我的开源C#代码,可以通过HTTP代理将文件上传到FTP。

        public bool UploadFile(string localFilePath, string remoteDirectory)
    {
        var fileName = Path.GetFileName(localFilePath);
        string content;
        using (var reader = new StreamReader(localFilePath))
            content = reader.ReadToEnd();

        var proxyAuthB64Str = Convert.ToBase64String(Encoding.ASCII.GetBytes(_proxyUserName + ":" + _proxyPassword));
        var sendStr = "PUT ftp://" + _ftpLogin + ":" + _ftpPassword
            + "@" + _ftpHost + remoteDirectory + fileName + " HTTP/1.1\n"
            + "Host: " + _ftpHost + "\n"
            + "User-Agent: Mozilla/4.0 (compatible; Eradicator; dotNetClient)\n" + "Proxy-Authorization: Basic " + proxyAuthB64Str + "\n"
            + "Content-Type: application/octet-stream\n"
            + "Content-Length: " + content.Length + "\n"
            + "Connection: close\n\n" + content;

        var sendBytes = Encoding.ASCII.GetBytes(sendStr);

        using (var proxySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
        {
            proxySocket.Connect(_proxyHost, _proxyPort);
            if (!proxySocket.Connected)
                throw new SocketException();
            proxySocket.Send(sendBytes);

            const int recvSize = 65536;
            var recvBytes = new byte[recvSize];
            proxySocket.Receive(recvBytes, recvSize, SocketFlags.Partial);

            var responseFirstLine = new string(Encoding.ASCII.GetChars(recvBytes)).Split("\n".ToCharArray()).Take(1).ElementAt(0);
            var httpResponseCode = Regex.Replace(responseFirstLine, @"HTTP/1\.\d (\d+) (\w+)", "$1");
            var httpResponseDescription = Regex.Replace(responseFirstLine, @"HTTP/1\.\d (\d+) (\w+)", "$2");
            return httpResponseCode.StartsWith("2");
        }
        return false;
    }

#11


-1  

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.

它奏效了。是的,我知道这个解决方案不是最好的解决方案。更重要的是,我不明白为什么它有效。但无论如何,目标是完整的。

#12


-1  

I'm not sure if all HTTP proxies work in the same way, but I managed to cheat ours by simply creating an HTTP request to access resource on URI ftp://user:pass@your.server.com/path.

我不确定所有HTTP代理是否都以相同的方式工作,但我设法通过简单地创建HTTP请求来访问URI ftp:// user:pass@your.server.com/path上的资源来欺骗我们。

Sadly, to create an instance of HttpWebRequest you should use WebRequest.Create. And if you do that you can't create an HTTP request for ftp:// schema.

遗憾的是,要创建HttpWebRequest的实例,您应该使用WebRequest.Create。如果这样做,则无法为ftp:// schema创建HTTP请求。

So I used a bit of reflection to invoke a non-public constructor which does that:

所以我使用了一些反射来调用非公共构造函数来执行此操作:

var ctor = typeof(HttpWebRequest).GetConstructor(
    BindingFlags.NonPublic | BindingFlags.Instance, 
    null, 
    new Type[] { typeof(Uri), typeof(ServicePoint) }, 
    null);
var req = (WebRequest)ctor.Invoke(new object[] { new Uri("ftp://user:pass@host/test.txt"), null });
req.Proxy = new WebProxy("myproxy", 8080);
req.Method = WebRequestMethods.Http.Put;

using (var inStream = req.GetRequestStream())
{
    var buffer = Encoding.ASCII.GetBytes("test upload");
    inStream.Write(buffer, 0, buffer.Length);
}

using (req.GetResponse())
{
}

You can also use other methods like "DELETE" for other tasks.

您还可以使用其他方法(如“DELETE”)执行其他任务。

In my case, it worked like a charm.

就我而言,它就像一个魅力。

#13


-5  

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代理使用不同的协议。

相关文章