(Related to: FTP directory listing returned as HTML instead of simple Linux ls output)
How can I force a C# program (FtpWebRequest) to use a direct IP to get onto the Internet instead of going through an HTTP proxy? (My knowledge of IT networks and related terminology is scant. Apologies in advance.)
To break out from the internal company network there is
- Indirect way via the IP of a (HTTP) proxy server.
- Direct IP of an internet service (not a proxy server).
I know this because, when using FileZilla with / without the proxy set in Internet Explorer, then on the remote FTP server, the logs either show the IP of the proxy, or the direct IP.
Code using C# FtpWebRequest to connect to FTP server outside of the company.
FtpWebRequest request = WebRequest.Create(uri) as FtpWebRequest;
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = server.Credential;
request.KeepAlive = true;
request.UsePassive = true;
request.EnableSsl = false;
//proxy options
//1.
// do nothing
//2.
request.Proxy = null;
//3. setup HTTP proxy
request.Proxy = new WebProxy(proxyuri, true);
request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
request.Proxy.Credentials = new NetworkCredential("Username", "Password");
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
while (!reader.EndOfStream)
{ ... }
When I set the proxy, then the C# program (obviously) uses the proxy server to get to the FTP server. However because this is an HTTP proxy, the directory listing is returned as HTML and deleting, uploading, and making directories is not possible. The program needs to delete files, create folders on the FTP.
Similarly, when not setting the proxy, then the program uses the settings in Internet Explorer, where the proxy is set, which then again uses the HTTP proxy.
In the code, setting the proxy to null (request.Proxy = null
OR WebRequest.DefaultWebProxy
OR GlobalProxySelection.GetEmptyWebProxy()
OR new WebProxy();
) causes an exception "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)."
This problem only occurs for one particular external FTP server (that happens to be running vsftpd). I tested using a different external FTP and both the proxy and the non proxy / direct connection work.
Questions 1. It seems the C# program can only breakout using the proxy. Why, when the proxy is not set, the program does not use the direct IP? 2. How can I force the program to use the direct IP? 3. Could the problem be due to the FTP server (vsftpd)?