C# HttpWebRequest使用代理凭据未将凭据传递给代理服务器

3

目前我正在按照类似以下步骤进行:

 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 proxy,并使用相关的模块类来创建URL和凭据,或者像上面那样),代理服务器都没有传递这些凭据。
我可能错过了什么吗?我尝试在默认应用程序池标识下从VS2010 Web应用程序中以调试模式运行此应用程序,以及从我们QA环境中的服务器运行相同的应用程序,但结果都没有改变- 它要么根本不使用代理,要么只是不发送凭据。
我还尝试将PreAuthenticate = true和UseDefaultCredentials both设置为true或k,但没有任何变化。
对于我错过的内容有什么想法吗?
1个回答

1

编辑:抱歉,一开始误解了您的问题-以下是更正的答案:

我建议尝试创建一个新的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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接