2009-07-29 62 views
12

我想弄清楚如何在使用System.Net.WebClient類時穩健地處理代理驗證錯誤(HTTP 407狀態碼)。System.Net.WebClient與代理驗證407錯誤

在該字段中,我們看到許多用戶接收到407代理身份驗證 WebException,但我不確定什麼是良好的默認策略。在.Net 2.0/3.5中,代理驗證設置應該從Internet Explorer系統設置的 繼承。 Firefox,Opera和Chrome使用這些 相同的設置。

下面是我們使用的基本代碼:

using System.Net; 

string url = "http://www.mysite.com"; 
WebClient webClient = new WebClient(); 
byte[] data = webClient.DownloadFile(url); 

當此代碼失敗,我們打開用戶的瀏覽器,並將它們發送到幫助 頁面。從我們的網絡日誌中,我們知道這些客戶可以在其瀏覽器中成功連接 。也許他們在他們到達我們的幫助頁面之前手動輸入他們的代理用戶名和密碼 ?我們不知道。

看來我們可以使用WebClient.UseDefaultCredentials,但是如果WebClient使用系統設置,這個 似乎是多餘的。

任何幫助表示讚賞。

回答

11

Internet Explorer不會持續緩存/重用代理身份驗證憑據如果代理身份驗證使用基本或摘要。對於Negotiate/NTLM,將提供默認憑據。因此,即使.NET繼承自IE設置,除非您恰好在IE中運行,否則您不會獲得Basic/Digest代理身份驗證的任何「免費」支持;您需要提示用戶或提供配置屏幕。

Fiddler(www.fiddler2.com)在「規則」菜單上具有「請求代理身份驗證」選項,您可以使用它來模擬此方案進行測試。

+2

謝謝埃裏克。小提琴奏效。 澄清,對於Negotiate/NTML,無論是否設置UseDefaultCredentials = true,WebClient都將提供默認憑據?換句話說,在處理407 WebException並使用UseDefaultCredentials = true重試時是否有價值?爲什麼不總是設置UseDefaultCredentials = true?安全風險? 意見聲明:WinInet似乎處理得更好。鑑於宇宙中的每個人都想要遍歷代理,爲什麼不自動執行此操作或者觸發事件並提供用於輸入/保存證書的標準UI? – Daniel 2009-07-31 20:32:46

+0

我使用Fiddler進行了更多實驗。我認爲只要提供像下面這樣的用戶名和密碼(「1」,「1」)很容易,但它不起作用。 string url =「http://www.java.com/」; webClient = new WebClient(); CredentialCache cache = new CredentialCache(); cache.Add(new Uri(url),「Basic」,new NetworkCredential(「1」,「1」)); webClient.Proxy.Credentials = credentialCache; webClient.Credentials = credentialCache; string contents = webClient.DownloadString(url); – Daniel 2009-07-31 23:47:32

+1

得到它的工作。由於某種原因,CredentialCache不起作用。我必須將NetworkCredential傳遞給webClient.Proxy.Credentials。 – Daniel 2009-08-01 00:02:34

6

我們通過添加alows用戶選擇「使用代理」一配置對話框中解決了這個問題。 如果這個設置完成,我們使用這些參數(地址,憑證...)。 如果不是 - 我們假設可以在沒有任何手動交互的情況下建立連接。 在一個錯誤的情況下,我們做到: a)使用默認憑據 b再試一次)彈出一個信息,在配置設置可以幫助...

如果代理身份驗證是通過「默認憑證」進行。 (Windows用戶)IE也會對auth錯誤做出反應,並在這種情況下發送默認憑據。 如果這不起作用,它將打開憑證對話框。 我不確定是否所有瀏覽器都以這種方式處理 - 但您可以簡單地使用fiddler嘗試一下,這樣您就可以看到發生了什麼。

+0

感謝您的詳細工作流程。 – 2010-10-07 22:07:14

6

我知道這是一箇舊的帖子,但我有一個類似的問題,試圖通過代理服務器將SSIS 2008R2(SQL Server Integration Services)腳本任務(VB.NET代碼)中的WebClient使用WebClient下載到遠程網站通過SSL進行安全保護,這也需要驗證

花了一段時間來找到解決的辦法和這個職位上幫助代理方。以下是適用於我的腳本代碼。可能對搜索相似的人有用。

Dim objWebClient As WebClient = New WebClient() 
    Dim objCache As New CredentialCache() 

    'https://www.company.net/xxxx/resources/flt 
    Dim strDownloadURL As String = Dts.Variables("FileURL").Value.ToString 

    '[email protected] 
    Dim strLogin As String = Dts.Variables("FileLogin").Value.ToString 

    'sitepassword 
    Dim strPass As String = Dts.Variables("FilePass").Value.ToString 

    'itwsproxy.mycompany.com 
    Dim strProxyURL As String = Dts.Variables("WebProxyURL").Value.ToString 

    '8080 
    Dim intProxyPort As Integer = Dts.Variables("WebProxyPort").Value 

    'Set Proxy & Credentials as a Network Domain User acc to get through the Proxy 
    Dim wp As WebProxy = New WebProxy(strProxyURL, intProxyPort) 
    wp.Credentials = New NetworkCredential("userlogin", "password", "domain") 
    objWebClient.Proxy = wp 

    'Set the Credentials for the Remote Server not the Network Proxy 
    objCache.Add(New Uri(strDownloadURL), "Basic", New NetworkCredential(strLogin, strPass)) 
    objWebClient.Credentials = objCache 

    'Download file, use Flat File Connectionstring to save the file 
    objWebClient.DownloadFile(strDownloadURL, Dts.Connections("XMLFile").ConnectionString) 
相關問題