C# FTP behind Proxy with FtpWebRequest [duplicate] - c#

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

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

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.

Related

DotNet HttpClient.DefaultProxy Property not reading system settings on Win10

The docs indicate, that the default Proxy-Setup for the HttpClient will be determined through the presence of environmental variables and/or the system wide proxy settings.
I set the proxy-auth credentials like this:
setx http_proxy http://user:password#proxyIP:proxyPort/, and to be sure rebootet the system.
Since the Windows proxy-settings dialog does not allow to set username and password, i included there the proxy address and port.
Now i try to read them back though the CredentialCache with my C# app.
However the CredentialCache.DefaultCredentials are still empty and the proxy blocks and responds with 407 exceptions. Which is correct, since i cannot pass the credentials.
How do i have to setup the username and password to be able to read them from the CredentialCache?
I would like to be able to go this way, since very old code, long time ago shipped to customers, using a WebRequest object together with this settings is able to go through.:
UseDefaultCredentials = true;
Proxy = WebRequest.GetSystemWebProxy();
Proxy.Credentials = CredentialCache.DefaultCredentials;
Thats why i would like to also be able to write somethin to Windows that in turn gets read out of the Cache.

FtpWebRequest ListDirectory does not return hidden files

Using FtpWebRequest to list the contents of a directory; however, it's not showing the hidden files.
How do I get it to show the hidden files?
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp_root + path);
request.Method = WebRequestMethods.Ftp.ListDirectory;
FileZilla lists the hidden files correctly so I know the FTP server is returning that data to it. I just need to replicate that with FtpWebRequest. Or use a different library for it.
The FtpWebRequest which is provided by Microsoft does not perform all the operations neccessary for listing FTP, FTPS or SFTP site's directories.
A good solution would be to use some other dll's like WinScp, Ftp.dll which can provide you with some efficient and extra functionalities.
Some FTP servers fail to include hidden files to responses to LIST and NLST commands (which are behind the ListDirectoryDetails and ListDirectory).
One solution is to use MLSD command, to which FTP servers do return hidden files. The MLSD command is anyway the only right way to talk to an FTP server, as its response format is standardized (what is not the case with LIST).
But .NET framework/FtpWebRequest does not support the MLSD command.
For that you would have to use a different 3rd party FTP library.
For example with WinSCP .NET assembly you can use:
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "ftp.example.com",
UserName = "user",
Password = "mypassword",
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
RemoteDirectoryInfo directory = session.ListDirectory("/remote/path");
foreach (RemoteFileInfo fileInfo in directory.Files)
{
Console.WriteLine(
"{0} with size {1}, permissions {2} and last modification at {3}",
fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions,
fileInfo.LastWriteTime);
}
}
See documentation for Session.ListDirectory method.
WinSCP will use MLSD, if the server supports it. If not, it will try to use -a trick (described below).
(I'm the author of WinSCP)
If you are stuck with FtpWebRequest, you can try to use -a switch with the LIST/NLST command. While that's not any standard switch (there are no switches in FTP), many FTP servers do recognize it. And it makes them return hidden files.
To trick FtpWebRequest to add the -a switch to LIST/NLST command, add it to the URL:
WebRequest.Create("ftp://ftp.example.com/remote/path/ -a");

How to transfer a file from a server to another on the same domain using C#

I have tried multiple different ways of transferring a file from a server to another different server which is on the same domain.
No matter what I try I keep getting the incorrect username or bad password error, however when I try access the "\serverIP\c$" folder manually from the server I am able to access the folder correctly with the right username and password.
The first part of my code places the file from the local pc to the server where the application is hosted, and this works perfectly :
string path = Path.Combine(Server.MapPath("~/ACAD_Drawings"),
Path.GetFileName(file.FileName));
file.SaveAs(path);
However I then need to move this file from this server onto a different server which will be using the file, and my last attempt was carried out using the following code:
NetworkCredential myCred = new NetworkCredential("Username", "Password", "DomainName");
WebClient webclient = new WebClient();
webclient.Credentials = myCred;
string tempFileForStorage = path;
file.SaveAs(tempFileForStorage);
webclient.UploadFile("\\\\NewServerIP\\c$", "PUT", tempFileForStorage);
webclient.Dispose();
System.IO.File.Delete(tempFileForStorage);
With this code I keep getting the incorrect username and password when I am sure that they are correct. Would anyone know if I am doing anything wrong or missing any steps?
1st in your quest: you need know SMB/CIFS protocol. See : https://en.wikipedia.org/wiki/Server_Message_Block , how to connect(#Steve Drake) : Connect to network drive with user name and password
2nd when you connect on another computer use SMB, you can read/write remote files use normal IO like local computer.
3nd if two servers are not in a same LAN, use other way to transfer files will be better, like socket, WebAPI etc.

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

Programmatically Set Proxy Address, Port, User, Password throught Windows Registry

I'm writing a small C# application that will use Internet Explorer to interact with a couple a websites, with help from WatiN.
However, it will also require from time to time to use a proxy.
I've came across Programmatically Set Browser Proxy Settings in C#, but this only enables me to enter a proxy address, and I also need to enter a Proxy username and password. How can I do that?
Note:
It doesn't matter if a solution changes the entire system Internet settings. However, I would prefer to change only IE proxy settings (for any connection).
The solution has to work with IE8 and Windows XP SP3 or higher.
I'd like to have the possibility to read the Proxy settings first, so that later I can undo my changes.
EDIT
With the help of the Windows Registry accessible through Microsoft.Win32.RegistryKey, i was able to apply a proxy something like this:
RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
registry.SetValue("ProxyEnable", 1);
registry.SetValue("ProxyServer", "127.0.0.1:8080");
But how can i specify a username and a password to login at the proxy server?
I also noticed that IE doesn't refresh the Proxy details for its connections once the registry was changed how can i order IE to refresh its connection settings from the registry?
Thanks
For IE, you can use the same place in the registry. Just set ProxyServer="user:password#127.0.0.1:8080" however firefox completely rejects this, and does not attempt to connect.
Here's how I've done it when accessing a web service through a proxy:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetUrl);
WebProxy proxy = new WebProxy(proxyUrl, port);
NetworkCredential nwCred = new NetworkCredential(proxyUser, proxyPassword);
CredentialCache credCache = new CredentialCache();
credCache.Add(proxy.Address, "Basic", nwCred);
credCache.Add(proxy.Address, "Digest", nwCred);
credCache.Add(proxy.Address, "Negotiate", nwCred);
credCache.Add(proxy.Address, "NTLM", nwCred);
proxy.Credentials = credCache;
proxy.BypassProxyOnLocal = false;
request.Proxy = proxy;

Categories

Resources