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

C# 无法连接到ftp:(407)需要代理身份验证,c#,.net,ftp,C#,.net,Ftp,我需要连接到ftp并从中下载文件,但我无法连接到它。我已指定凭据,可以通过浏览器连接,但不能通过.NET连接 FtpWebRequest downloadRequest = (FtpWebRequest)WebRequest.Create(ftpSite + "/" + TOC); downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile; downloadRequest.Credent

我需要连接到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
,我不太清楚原因。我的老板也很困惑。有什么见解吗?

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


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

您很可能坐在需要身份验证的FTP代理后面

尝试初始化

downloadRequest.Proxy
具有适当的代理凭据,例如

WebProxy proxy = new WebProxy("http://proxy:80/", true); 
proxy.Credentials = new NetworkCredential("userId", "password", "Domain"); 
downloadRequest.Proxy = proxy;
使用
useDefaultCredentials=“true”
元素放入

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

此[post][1]可能有帮助[1]: