简体   繁体   中英

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. I've specified the credentials and am able to connect through my browser, but not through .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. My boss is baffled as well. Any insight?

Did you try to use the command-line FTP client to do it?

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.

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. 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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