简体   繁体   English

无法连接到 ftp:(407) 需要代理身份验证

[英]Unable to connect to ftp: (407) Proxy Authentication Required

I need to connect to an ftp and download a file from it, but I'm unable to connect to it.我需要连接到 ftp 并从中下载文件,但我无法连接到它。 I've specified the credentials and am able to connect through my browser, but not through .NET.我已经指定了凭据并且能够通过我的浏览器进行连接,但不能通过 .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();

It throws a WebException with the 407 error and I'm not quite sure why.它抛出一个带有 407 错误的WebException ,我不太清楚为什么。 My boss is baffled as well.我的老板也很困惑。 Any insight?有什么见解吗?

Did you try to use the command-line FTP client to do it?您是否尝试使用命令行 FTP 客户端来执行此操作?

I expect that the error message you got already explains the problem - you're behind an application-level firewall which requires you to authenticate using the proxy.我希望您收到的错误消息已经解释了问题 - 您位于应用程序级防火墙后面,需要您使用代理进行身份验证。 Your browser is presumably already configured to do this.您的浏览器可能已经配置为执行此操作。 Consult your.network administrator.请咨询您的网络管理员。

Most likely you are sitting behind an FTP proxy that requires authentication.您很可能坐在需要身份验证的 FTP 代理后面。

Try initializing尝试初始化

downloadRequest.Proxy

with appropriate proxy credentials, eg something like具有适当的代理凭据,例如类似

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

Putting a <defaultProxy /> element into in app.config / web.config under <system.net> with useDefaultCredentials="true" may well help.<defaultProxy />元素放入<system.net>下的 app.config / web.config 中并使用useDefaultCredentials="true"可能会有帮助。 Depending on your.network setup, this may avoid you needing to hard code proxy account details in your app.根据您的网络设置,这可能会避免您需要在应用中硬编码代理帐户详细信息。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM