2012-04-05 123 views
0

我需要連接到ftp並從中下載文件,但我無法連接到它。我已經指定了憑據,並且能夠通過我的瀏覽器進行連接,但不能通過.NET進行連接。無法連接到ftp:(407)需要代理驗證

 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(); 

它拋出一個WebException與407錯誤,我不知道爲什麼。我的老闆也很困惑。任何見解?

+0

這[文章] [1]可以幫助 [1]:http://stackoverflow.com/questions/1524566/407-proxy-authentication-required – mreyeros 2012-04-05 19:19:55

回答

1

很可能你坐在一個需要驗證的FTP代理之後。

嘗試初始化

downloadRequest.Proxy 

與適當的代理證書,例如像

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

您是否嘗試使用命令行FTP客戶端來做到這一點?

我期望您收到的錯誤消息已經解釋了問題 - 您身後的應用程序級防火牆需要您使用代理進行身份驗證。您的瀏覽器大概已經配置爲執行此操作。諮詢您的網絡管理員。

0

把一個<defaultProxy />元素成的app.config/web.config中<system.net>下與useDefaultCredentials="true"可能幫助。根據您的網絡設置,這可能會避免您需要在應用程序中硬編碼代理帳戶詳細信息。

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