【问题标题】:Strange behaviour from FTPWebRequest-Class来自 FTPWebRequest-Class 的奇怪行为
【发布时间】:2017-01-26 17:51:14
【问题描述】:

我在使用 FtpWebRequest-Class 时发现了一个奇怪的行为。我想创建一个 FtpWebRequest 从德国气象服务处获取一些数据。所以我做某事。像下面这样:

var request = (FtpWebRequest)WebRequest.Create(requestUri);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Credentials = Credentials;
var response = request.GetResponse() as FtpWebResponse;

使用此代码,我收到以下错误:

远程服务器返回错误:(407) Proxy Authentication 必填

这很清楚,我认为添加代理可以解决这个问题,但是我发现了一些奇怪的东西。此代码有效:

var request = (FtpWebRequest)WebRequest.Create(requestUri);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Credentials = Credentials;
request.Proxy = null;
var response = request.GetResponse() as FtpWebResponse;

有人可以解释一下吗?我使用调试器来保存代理在这两种情况下都是空的。为什么我在第二种情况下没有收到错误消息,反之亦然?

【问题讨论】:

    标签: c# .net ftp


    【解决方案1】:

    默认情况下,WebRequest 使用系统范围的代理(在 IE 设置中定义的代理)。如果该代理需要凭据,您需要提供它们。通过将Proxy 设置为null,您可以完全绕过代理,使用直接连接。

    您可以在the code 中看到这一点。当您设置代理时,它sets a private m_ProxyUserSet 标志为true,在决定要使用的实际代理时为checked later。如果你没有明确指定代理,它会隐式使用WebRequest.InternalDefaultWebProxy

    【讨论】:

      猜你喜欢
      • 2020-02-10
      • 2014-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多