我目前正在关注与此类似的内容:
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运行。与我们质量检查环境中的服务器相同,并且没有任何变化-它要么根本不使用代理,要么不发送凭证。
我也尝试将
PreAuthenticate = true
和UseDefaultCredentials
都设置为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;
关于c# - 具有代理凭据的C#HttpWebRequest没有将凭据传递给代理服务器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15503061/