如何强制C#FtpWebRequest使用直接IP访问Internet而不是通过HTTP代理? [英] How can I force a C# FtpWebRequest to use a direct IP to get onto the Internet instead of going through an HTTP proxy?

查看:208
本文介绍了如何强制C#FtpWebRequest使用直接IP访问Internet而不是通过HTTP代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(相关: FTP目录列表返回为HTML而不是简单的Linux ls输出

上网而不是通过HTTP代理? (我对IT网络和相关术语的了解很少,提前抱歉)

为了突破内部公司网络,有


  1. 通过(HTTP)代理服务器的IP的间接方式。

  2. 互联网服务的直接IP(不是代理服务器) 。

我知道这一点,因为在Internet Explorer中使用/不使用代理服务器时使用FileZilla,然后在远程FTP服务器上日志要么显示代理的IP,要么显示直接IP。



使用C#FtpWebRequest连接到公司以外的FTP服务器的代码。 b

  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;

//代理选项
// 1。
//不做任何事

// 2。
request.Proxy = null;

// 3。设置HTTP代理
request.Proxy = new WebProxy(proxyuri,true);
request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
request.Proxy.Credentials = new NetworkCredential(用户名,密码);

Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
while(!reader.EndOfStream)
{...}

我设置了代理,然后C#程序(显然)使用代理服务器到达FTP服务器。但是,因为这是一个HTTP代理,所以目录列表以HTML格式返回,并且不能删除,上传和制作目录。该程序需要删除文件,在FTP上创建文件夹。



同样,当不设置代理时,程序将使用Internet Explorer中的设置,代理位于set,然后再次使用HTTP代理。



在代码中,将代理设置为null( request.Proxy = null OR WebRequest.DefaultWebProxy GlobalProxySelection.GetEmptyWebProxy() new WebProxy() ; )导致一个异常远程服务器返回一个错误:(550)文件不可用(例如,文件未找到,无法访问)。



只有一个特定的外部FTP服务器(恰好在运行vsftpd)才会出现此问题。我使用不同的外部FTP进行了测试,包括代理服务器和非代理/直接连接工作。

问题
1.看来C#程序只能使用代理进行分解。为什么当代理没有设置时,程序不使用直接IP?
2.我如何强制程序使用直接IP?
3.该问题是由于FTP服务器(vsftpd)造成的吗?

解决方案

systemroot%\system32\drivers\etc)有错误的条目。我并不确定哪个入口是问题,但文件现在只包含一行

  127.0.0.1 localhost 


(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)?

解决方案

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

这篇关于如何强制C#FtpWebRequest使用直接IP访问Internet而不是通过HTTP代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆