WebClient 类的域凭据不起作用
我正在尝试通过 C# 代码获取网站的 HTML 源代码。当我使用 Windows 身份验证访问该站点时,以下代码有效:
using (WebClient client = new WebClient())
{
client.Credentials = CredentialCache.DefaultCredentials;
using (Stream stream = client.OpenRead("http://intranet/"))
using (StreamReader reader = new StreamReader(stream))
{
MessageBox.Show(reader.ReadToEnd());
}
}
当我手动输入域凭据时,我收到一条“未经身份验证”消息。
using (WebClient client = new WebClient())
{
NetworkCredential credentials = new NetworkCredential("username", "pass", "domain");
client.Credentials = credentials;
using (Stream stream = client.OpenRead("http://intranet/"))
using (StreamReader reader = new StreamReader(stream))
{
MessageBox.Show(reader.ReadToEnd());
}
}
为什么会这样呢?
I'm trying to get a HTML source of a website through C# code. When I access the site with Windows Authentication, the following code works:
using (WebClient client = new WebClient())
{
client.Credentials = CredentialCache.DefaultCredentials;
using (Stream stream = client.OpenRead("http://intranet/"))
using (StreamReader reader = new StreamReader(stream))
{
MessageBox.Show(reader.ReadToEnd());
}
}
When I enter my domain credentials manually, I get an "unauthenticated" message.
using (WebClient client = new WebClient())
{
NetworkCredential credentials = new NetworkCredential("username", "pass", "domain");
client.Credentials = credentials;
using (Stream stream = client.OpenRead("http://intranet/"))
using (StreamReader reader = new StreamReader(stream))
{
MessageBox.Show(reader.ReadToEnd());
}
}
Why is it so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
Try this: