FTP File Upload with HTTP Proxy

前端 未结 13 1817
轻奢々
轻奢々 2020-12-05 07:48

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

相关标签:
13条回答
  • 2020-12-05 08:07

    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!

    0 讨论(0)
  • 2020-12-05 08:08

    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.

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

    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.

    0 讨论(0)
  • 2020-12-05 08:09

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

            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;
        }
    
    0 讨论(0)
  • 2020-12-05 08:14

    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)

    0 讨论(0)
  • 2020-12-05 08:18

    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.

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

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

    0 讨论(0)
  • 2020-12-05 08:18

    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.

    0 讨论(0)
提交回复
热议问题