MGZero
MGZero

Reputation: 5963

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?

Upvotes: 0

Views: 6129

Answers (3)

tomfanning
tomfanning

Reputation: 9670

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>

Upvotes: 0

Eric J.
Eric J.

Reputation: 150138

Most likely you are sitting behind an FTP proxy that requires authentication.

Try initializing

downloadRequest.Proxy

with appropriate proxy credentials, e.g. something like

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

Upvotes: 1

James Youngman
James Youngman

Reputation: 3733

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.

Upvotes: 1

Related Questions