【问题标题】:Unable to connect to ftp: (407) Proxy Authentication Required无法连接到 ftp:(407) 需要代理验证
【发布时间】:2012-04-05 19:13:07
【问题描述】:

我需要连接到一个 ftp 并从中下载一个文件,但我无法连接到它。我已经指定了凭据并且能够通过我的浏览器进行连接,但不能通过 .NET。

        FtpWebRequest downloadRequest = (FtpWebRequest)WebRequest.Create(ftpSite + "/" + TOC);
        downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile;
        downloadRequest.Credentials = new NetworkCredential(userName, pass);
        FtpWebResponse response = (FtpWebResponse)downloadRequest.GetResponse(); //execption thrown here
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);

        reader.ReadLine();

        while (!reader.EndOfStream)
            data.Add(reader.ReadLine());

        reader.Close();

它会抛出带有 407 错误的 WebException,我不太清楚为什么。我的老板也很困惑。有什么见解吗?

【问题讨论】:

标签: c# .net ftp


【解决方案1】:

您很可能正在使用需要身份验证的 FTP 代理。

尝试初始化

downloadRequest.Proxy

使用适当的代理凭据,例如像

WebProxy proxy = new WebProxy("http://proxy:80/", true); 
proxy.Credentials = new NetworkCredential("userId", "password", "Domain"); 
downloadRequest.Proxy = proxy;

【讨论】:

    【解决方案2】:

    您是否尝试使用命令行 FTP 客户端来执行此操作?

    我希望您收到的错误消息已经说明了问题 - 您位于应用程序级防火墙后面,该防火墙要求您使用代理进行身份验证。您的浏览器可能已经配置为执行此操作。请咨询您的网络管理员。

    【讨论】:

      【解决方案3】:

      <defaultProxy /> 元素放入<system.net> 下的app.config / web.config 和useDefaultCredentials="true" 可能会有所帮助。根据您的网络设置,这可能避免您需要在应用中硬编码代理帐户详细信息。

      <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
        <system.net>
          <defaultProxy useDefaultCredentials="true" />
        </system.net>
      </configuration>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多