使用 HttpWebRequest 进行网络身份验证和网站身份验证
我正在尝试创建一个使用 .NET Framework 使用 RSS 数据的应用程序。 RSS 站点需要用户名和密码才能启动。 我在我的工作场所运行此应用程序,需要 NTLM 身份验证才能连接到互联网。
以下是我尝试使用的代码,
NetworkCredential nc = new NetworkCredential("SITEUSERNAME", "SITEPASSWORD");
CredentialCache cache = new CredentialCache();
cache.Add(new Uri(RSSFeed), "Basic", nc);
cache.Add(new Uri(RSSFeed), "Ntlm", new NetworkCredential("USERNAME","PASSWORD","DOAMIN"));
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(RSSFeed);
myHttpWebRequest.Proxy.Credentials = cache;
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
我收到 407 错误,如果我只是使用 CredentialCache.DefaultNetworkCredentials,我收到 401 错误。
I am trying to create a application that will consume RSS data using .NET Framework. The RSS site requires User name and Password to start with.
and Am running this application from within my work place which requires NTLM authentication to connect to internet.
Following is the code that i am trying to use
NetworkCredential nc = new NetworkCredential("SITEUSERNAME", "SITEPASSWORD");
CredentialCache cache = new CredentialCache();
cache.Add(new Uri(RSSFeed), "Basic", nc);
cache.Add(new Uri(RSSFeed), "Ntlm", new NetworkCredential("USERNAME","PASSWORD","DOAMIN"));
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(RSSFeed);
myHttpWebRequest.Proxy.Credentials = cache;
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
i get 407 error and if i simply use CredentialCache.DefaultNetworkCredentials i get 401 error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用以下代码解决了该问题
solved the issue by using following code
如果这段代码有效,那么上面的原始代码是错误的。您应该设置
,
换句话说,您已经交换了代理和目标服务器的凭据。
If this code works, then your original code above was wrong. You should set
and
In other words, you had exchanged credentials for the proxy and the destination server.