1
votes

(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

  1. Indirect way via the IP of a (HTTP) proxy server.
  2. 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)?

1
There is a good chance you won't be able to connect directly. The proxy might be acting as the gateway.Dreamwalker
But I can tell IE to use the proxy and surf the web AND I can tell IE to not use the proxy and still surf the web.robor

1 Answers

1
votes

The windows hosts file (%systemroot%\system32\drivers\etc) had erroneous entries. I am not exactly sure which entry was the problem but the file now only contains one line

127.0.0.1       localhost