簡體   English   中英

具有代理憑據的C#HttpWebRequest未將憑據傳遞給代理服務器

[英]C# HttpWebRequest with Proxy Credentials not passing credentials to proxy server

我目前正在關注類似的事情:

 HttpWebRequest myWebRequest = (HttpWebRequest) WebRequest.Create("http://www.microsoft.com");
IWebProxy proxy = myWebRequest.Proxy;
Uri newUri = new Uri(proxyAddress);
myProxy.Address = newUri;
... (setting of username/password for proxy)
myProxy.Credentials = new NetworkCredential(username, password);
 myWebRequest.Proxy = myProxy;

HttpWebResponse myWebResponse = (HttpWebResponse) myWebRequest.GetResponse();

我正在運行IIS 7.5托管應用程序中的代碼,該應用程序需要在進程繼續之前驗證URL是否可聯系。 由於服務不應該訪問外部世界,我需要使用具有我們的IT部門提供給我的憑據的特定代理服務器。

不幸的是,無論我嘗試什么(app.config中的system.net/default代理,使用關聯的模塊類來創建URL和憑據,或者類似上面的內容),代理服務器都不會將那些憑據傳遞給它。

有什么我可能錯過的嗎? 我已嘗試在調試模式下從VS2010 Web應用程序運行此應用程序,其中應用程序從IIS運行,具有默認的應用程序池標識。 並且在我們的QA環境中的服務器中沒有任何變化 - 它根本不使用代理,或者只是不發送憑據。

我也嘗試將PreAuthenticate = trueUseDefaultCredentials設置為true和k,並且沒有變化。

關於我缺少的任何想法?

編輯:對不起,首先誤解了您的問題 - 更正了以下答案:

我會嘗試創建一個新的WebProxy對象,設置其憑據,然后將其設置為您的請求的代理(而不是從請求中獲取現有的代理對象並進行修改):

HttpWebRequest myWebRequest =
    (HttpWebRequest) WebRequest.Create("http://www.microsoft.com");
IWebProxy proxy = new WebProxy(proxyAddress);
string proxyUsername = @"foo";
string proxyPassword = @"bar";
proxy.Credentials = new NetworkCredential(proxyUsername, proxyPassword);
myWebRequest.Proxy = proxy;

暫無
暫無

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

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