gpt4 book ai didi

c# - WebClient 类的域凭据不起作用

转载 作者:行者123 更新时间:2023-12-03 10:10:39 27 4
gpt4 key购买 nike

我正在尝试通过 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());
}
}
为什么会这样?

最佳答案

尝试这个:

CredentialCache cc = new CredentialCache();
cc.Add(
new Uri("http://intranet/"),
"NTLM",
new NetworkCredential("username", "pass", "domain"));
client.Credentials = cc;

关于c# - WebClient 类的域凭据不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1680718/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com