0

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?

MGZero
  • 5,812
  • 5
  • 29
  • 46
  • This [post][1] may help [1]: http://stackoverflow.com/questions/1524566/407-proxy-authentication-required – mreyeros Apr 05 '12 at 19:19

3 Answers3

1

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.

James Youngman
  • 3,623
  • 2
  • 19
  • 21
1

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;
Eric J.
  • 147,927
  • 63
  • 340
  • 553
0

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>
tomfanning
  • 9,552
  • 4
  • 50
  • 78