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 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:
USER commands for proxy user and target host userOPEN commandSITE commandIn 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With