简体   繁体   中英

Is it possible to use C# FtpWebRequest through a FTP proxy?

As I understand it, the FtpWebRequest.Proxy property denotes an HTTP proxy. I have to issue FTP requests to an external server via an FTP proxy.

The only way I've got this to work so far is by creating a script which uses the Windows FTP command and downloading that way.

Is it possible to use the FtpWebRequest to download files via an FTP proxy?

Here's code I've used before, I should caveat I've only tested this against a Checkpoint firewall so the format USER and PASS commands maybe different for your FTP Proxy. Your system administrator will know the correct format.

FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(
       new Uri("ftp://FTP PROXY HOST/actual/path/to/file/on/remote/ftp/server"));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;

reqFTP.Credentials = new NetworkCredential
       ("REMOTE FTP USER@FTP PROXY USER@REMOTE FTP HOST"
       , "REMOTE FTP PASSWORD@FTP PROXY PASSWORD");

If you have the budget for it - Dart do some great classes for this:

http://www.dart.com/ or specifically http://www.dart.com/ptftpnet.aspx

If the FTP proxy allows specifying all information about the target via USER and PASS commands, you can use the Credentials property .

Typically, you specify the username in a form user@proxyuser@host and password in a form password@proxypassword :

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://proxy/path");
request.Credentials = new NetworkCredential("user@proxyuser@host", "password@proxypassword");

If the proxy does not require authentication, use a form user@host and password :

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://proxy/path");
request.Credentials = new NetworkCredential("user@host", "password");

But your proxy server may also require a different syntax, like:

  • separate USER commands for proxy user and target host user
  • OPEN command
  • SITE command

In these cases, you cannot use the FtpWebRequest . You must use a 3rd party FTP client library instead.

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

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

// Configure proxy
sessionOptions.AddRawSettings("ProxyHost", "proxy");
sessionOptions.AddRawSettings("FtpProxyLogonType", "2");
sessionOptions.AddRawSettings("ProxyUsername", "proxyuser");
sessionOptions.AddRawSettings("ProxyPassword", "proxypassword");

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

    // Your code
}

For the options for the SessionOptions.AddRawSettings , see raw settings .

Easier is to configure the proxy settings in WinSCP GUI and have it 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)

I know this is way past the due date, but yes, I'm able to get FtpWebRequest to work with an ftp proxy by using the old tricks of setting the URL to

"ftp://your.proxy.server/theFileToDownload")" 

Then set your network credential to

username="ftpUserName@ftp.realserver.com" 

and

password="password". 

I remembered doing this back in the bad old WININET days, and sometimes the old tricks are still the best tricks.

YMMV of course.

They claim it is possible to list, listdetails, and download through a proxy. However I could not get this to work with an isa firewall. So I disabled the default proxy in my app.config and added an application rule for ForeFront/ISA client. To do this I created a file c:\\programdata\\microsoft\\firewall client 2004\\application.ini with the contents:

[applicationName] DisableEx=0 Disable=0 NameResolution=R

where applicationName is the running exe minus the .exe extension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM