【问题标题】:How can I force a C# FtpWebRequest to use a direct IP to get onto the Internet instead of going through an HTTP proxy?如何强制 C# FtpWebRequest 使用直接 IP 访问 Internet,而不是通过 HTTP 代理?
【发布时间】:2015-01-28 08:17:23
【问题描述】:

(相关:FTP directory listing returned as HTML instead of simple Linux ls output)

如何强制 C# 程序 (FtpWebRequest) 使用直接 IP 访问 Internet,而不是通过 HTTP 代理? (我对 IT 网络和相关术语的了解很少。提前道歉。)

要从公司内部网络中突围

  1. 通过 (HTTP) 代理服务器 IP 的间接方式。
  2. 互联网服务的直接 IP(不是代理服务器)。

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

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

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

当我设置代理时,C# 程序(显然)使用代理服务器访问 FTP 服务器。但是,因为这是一个 HTTP 代理,所以目录列表以 HTML 形式返回,并且无法删除、上传和创建目录。程序需要删除文件,在FTP上创建文件夹。

类似地,当不设置代理时,程序会使用 Internet Explorer 中的设置,其中设置了代理,然后再次使用 HTTP 代理。

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

这个问题只发生在一个特定的外部 FTP 服务器上(它恰好正在运行 vsftpd)。我使用不同的外部 FTP 进行了测试,代理和非代理/直接连接都可以工作。

问题 1. C#程序似乎只能使用代理突破。为什么,在没有设置代理的情况下,程序不使用直接IP? 2.如何强制程序使用直接IP? 3. 会不会是FTP服务器(vsftpd)的问题?

【问题讨论】:

  • 您很有可能无法直接连接。代理可能充当网关。
  • 但是我可以告诉 IE 使用代理并上网,我可以告诉 IE 不使用代理并仍然上网。

标签: c# networking proxy ftp


【解决方案1】:

Windows 主机文件 (%systemroot%\system32\drivers\etc) 有错误条目。我不确定哪个条目有问题,但文件现在只包含一行

127.0.0.1       localhost

【讨论】:

    猜你喜欢
    • 2021-03-17
    • 2010-09-10
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多