簡體   English   中英

(407)需要代理驗證

[英](407) Proxy Authentication Required

我知道這已被問過很多次了。 我已經閱讀了這里的大部分帖子和其他類似的網站。

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設置連接到代理但仍然無法正常工作。 我也嘗試過代碼

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就好了。 目標是預先驗證代理,而無需在代碼中提供用戶名密碼。

@David Moore是對的。 如果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();

        }
    }
}

希望能幫助到你 ;-)

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

我會把machine.config恢復原樣。

我要檢查的一件事是在IIS中,您是否可以為Windows身份驗證小程序配置提供程序。 這應列出NTLM和Kerberos作為列表中的提供者; 我會切換它們,看看是否有所不同(例如,如果NTLM是列表的頂部,將Kerberos移到頂部)。 對不起,我不能給你准確的說明,因為我在這台機器上沒有IIS。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM