网络身份验证和使用网站身份验证的HttpWebRequest [英] network Authentication and website Authentication using HttpWebRequest

查看:595
本文介绍了网络身份验证和使用网站身份验证的HttpWebRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个应用程序,将使用.NET Framework使用RSS数据。 RSS的网站需要用户名和密码开始。
和我从我的工作地点在该应用程序需要NTLM身份验证连接到互联网。

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.

下面是我尝试使用code

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();

我得到407错误,如果我简单地使用CredentialCache.DefaultNetworkCredentials我得到401错误。

i get 407 error and if i simply use CredentialCache.DefaultNetworkCredentials i get 401 error.

推荐答案

如果这个code ++工程,那么上面你原来的code是错误的。你应该设置

If this code works, then your original code above was wrong. You should set

request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;

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.Credentials = cache;

在换句话说,你已经为代理和目标服务器交换凭据。

In other words, you had exchanged credentials for the proxy and the destination server.

这篇关于网络身份验证和使用网站身份验证的HttpWebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆