webclient 401 转换为 c#

发布于 2025-01-03 05:26:45 字数 418 浏览 2 评论 0原文

我在使用网络客户端时遇到一些问题。

当我尝试时:

var client = new WebClient();
client.Credentials = new NetworkCredential("intranet.homolog", "S3br@32011", "na-sebrae");
var html = client.DownloadData("http://www.intranet.sebrae.com.br/noticias/todas-as-notícias/rss.aspx?estado=");

我收到错误(401)。

该 url 返回 xml feed,当我在浏览器中访问它时,我可以正常登录。

该用户和密码是真实的。

有人有一些想法可以让我使用网络客户端访问它吗?

I'm have some problems to use webclient.

When I try it:

var client = new WebClient();
client.Credentials = new NetworkCredential("intranet.homolog", "S3br@32011", "na-sebrae");
var html = client.DownloadData("http://www.intranet.sebrae.com.br/noticias/todas-as-notícias/rss.aspx?estado=");

I get an error (401).

This url returns xml feed, and, when I access it into browser, I login normally.

This user, and password are real.

Somebody have some ideia to I access it with the webclient?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

來不及說愛妳 2025-01-10 05:26:45

我的猜测是:您误用了 NetworkCredential 构造函数。

正确的语法是

public NetworkCredential(
    string userName,
    string password,
    string domain
)

首先是用户名,然后是密码,然后是域 - 您的语法全错了。

请尝试以下操作:

var client = new WebClient();
client.Credentials = new NetworkCredential("na-sebrae", 
                                           "S3br@32011",  "intranet.homolog");
var html = client.DownloadData("http://www.intranet.sebrae.com.br" +
                               "/noticias/todas-as-notícias/rss.aspx?estado=");

Here's my guess: you're misusing the NetworkCredential constructor

The correct syntax is

public NetworkCredential(
    string userName,
    string password,
    string domain
)

First username, then password, then domain - you got yours all wrong.

Try the following:

var client = new WebClient();
client.Credentials = new NetworkCredential("na-sebrae", 
                                           "S3br@32011",  "intranet.homolog");
var html = client.DownloadData("http://www.intranet.sebrae.com.br" +
                               "/noticias/todas-as-notícias/rss.aspx?estado=");
违心° 2025-01-10 05:26:45

我也遇到同样的错误。相同的链接在浏览器中效果更好,但为 WebClient 提供 401 异常。

    string url = "http://www.intranet.sebrae.com.br/noticias/todas-as-notícias/rss.aspx?estado=";
    var webClient = new WebClient();

    webClient.Credentials = CredentialCache.DefaultCredentials;

    byte[] html = webClient.DownloadData(fileAbsoluteUri);

I too get same error. The same link work better in browser but but giving 401 exception for WebClient.

    string url = "http://www.intranet.sebrae.com.br/noticias/todas-as-notícias/rss.aspx?estado=";
    var webClient = new WebClient();

    webClient.Credentials = CredentialCache.DefaultCredentials;

    byte[] html = webClient.DownloadData(fileAbsoluteUri);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文