2013-03-29 111 views
2

我知道這已被問了很多次。我已經閱讀了大多數在這裏和其他網站上的所有帖子,比如這個。(407)需要代理驗證

http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/2931d21c-9ca8-4256-b213-915fad4c941b/

隨着無濟於事。這裏的環境

的Windows Server 2008 R2 64位 的Visual Studio 2008 .NET Framework 3.5的

這是我曾嘗試

我使用代碼

WebRequest req = WebRequest.Create(requestUri + data); 
req.Proxy = new System.Net.WebProxy(<ProxyURL>:<port>",true); 
req.Proxy.Credentials = CredentialCache.DefaultCredentials; 
WebResponse resp = req.GetResponse(); 

這不得不代理認證工作,但看到因爲它減慢了應用程序,我瞭解到我可以編輯我所做的machine.config文件。它也工作!

<system.net> 
     <defaultProxy 
     useDefaultCredentials="true"> 
     <proxy 
     proxyaddress="<proxyURL>:<port>" 
     bypassonlocal="True"/> 
    </defaultProxy> 
    </system.net> 

至少一天或2天然後它開始失敗。

我然後編輯這個

<system.net> 
    <defaultProxy 
     useDefaultCredentials="true"> 
     <proxy usesystemdefault="True"/> 
     </defaultProxy> 
    </system.net> 

從我的理解,這將使用IE設置連接到代理,但仍然無法正常工作。我也嘗試tihs代碼

WebProxy proxy = new WebProxy(<proxy>:<port>); 
CredentialCache myCache = new CredentialCache(); 
myCache.Add(new Uri(requestUri + data), "BASIC", new NetworkCredential(<username>,<password>)); 
proxy.Credentials = myCache; 
request.Proxy = proxy; 
request.Method = "GET"; 

而這沒有奏效。

注意:我可以將machine.config文件複製到我的電腦(Win XP)並從那裏運行.exe(沒有代理代碼),它工作正常。

與64位操作系統有什麼不同嗎?另外我可以在服務器上打開IE8並訪問URI就好了。目標是預先驗證代理,而不必在代碼中提供用戶名密碼。

+0

密碼不能包含特殊字符......試試吧! – 2013-12-25 10:22:44

回答

3

HttpWebRequest無論如何都使用默認的Internet設置(IE)代理,所以如果它在服務器上的Internet Explorer上正常工作,它也應該可以從您的代碼中運行(假設它在相同的用戶帳戶下運行)。

我會把machine.config放回原樣。

我會檢查的一件事情是在IIS中,您可以配置Windows身份驗證小程序的提供程序。這應該將NTLM和Kerberos列爲列表中的提供者;我會切換它們,看看它是否有所作爲(例如,如果NTLM位於列表的頂端,則將Kerberos移到頂端)。對不起,我不能給你確切的指示,因爲我沒有在這臺機器上的IIS。

如果您還在掙扎,我會建議您在服務器上運行Fiddler以捕獲請求和響應流以獲取更多線索。

+0

似乎沒有任何工作。檢查IIS,Windows身份驗證使用協商和NTLM,如果這有助於任何 – jlh3302

+1

無需代碼,它確實使用IE設置。我沒有工作,因爲我試圖使用提琴手,它改變了IE代理設置。在Fiddler中,我選擇Rules/Automatically Authenticate並且我的代碼有效。一旦我關閉Fiddler,我就檢查了IE代理設置,確保它們恢復正常。似乎仍然有效。 – jlh3302

5

@大衛摩爾是對的。如果IE瀏覽器工作正常,當你手動瀏覽,然後只需添加req.Proxy.Credentials = CredentialCache.DefaultCredentials;,它會正常工作。

繼承人修改後的代碼取自MSDN,爲我工作。

using System; 
using System.Diagnostics; 
using System.IO; 
using System.Net; 

namespace ConsoleApplication4 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string urlDemo = "http://en.wikipedia.org/wiki/Main_Page"; 
      // Create a request for the URL. 
      WebRequest request = WebRequest.Create(urlDemo); 
      // If required by the server, set the proxy credentials. 
      request.Proxy.Credentials = CredentialCache.DefaultCredentials; 
      // Get the response. 
      WebResponse response = request.GetResponse(); 
      // Display the status. 
      Console.WriteLine(((HttpWebResponse)response).StatusDescription); 
      // Get the stream containing content returned by the server. 
      Stream dataStream = response.GetResponseStream(); 
      // Open the stream using a StreamReader for easy access. 
      StreamReader reader = new StreamReader(dataStream); 
      // Read the content. 
      string responseFromServer = reader.ReadToEnd(); 
      // Display the content. 
      Console.WriteLine(responseFromServer); 
      Console.ReadLine(); 
      // Clean up the streams and the response. 
      reader.Close(); 
      response.Close(); 

     } 
    } 
} 

希望它可以幫助;-)

+0

嗯 - 這段代碼可以和沒有WebRequest.Proxy一起使用。所以我不確定展示的是什麼。 –

+0

我得到了407,我真的不應該得到它(或者我認爲),但隨後我添加了* req.Proxy.Credentials = CredentialCache.DefaultCredentials *行,正如你所建議的和Bingo!這解決了它。 – Cyberherbalist

相關問題