为 http://user:password@doamin/query Url 创建 HttpWebRequest

发布于 2024-12-22 18:56:59 字数 819 浏览 0 评论 0原文

我需要将 HttpWebRequest 发送到具有基本内联凭据的 url,如下所示:

http://user:password@doamin/query

我已尝试按原样设置 Url,但它似乎没有传递凭据(得到 403)。
尝试设置 HttpWebRequest 的 Credentials 属性:

request.Credentials = new NetworkCredentials("username","pasword")

并将它们从 url 中删除(生成 http://domain/query),但仍然得到相同的结果 (403)。

直接从任何浏览器使用 URL 均成功,因此凭据正常。

我缺少什么?

[更新 - 答案]
这是对我有用的代码:

string credentials = "username:password";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(formattedUrl);
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials)));
request.PreAuthenticate = true;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

I need to send a HttpWebRequest to a url with basic inline credentials as follows:

http://user:password@doamin/query

I've tried setting the Url as is, but it didnt seem to pass the credentials (got 403).
Tried setting the Credentials property of HttpWebRequest:

request.Credentials = new NetworkCredentials("username","pasword")

And removing them from the url (resulting in http://domain/query) but still got the same result (403).

Using the Url directly from any browser succeeded so the credentials are Ok.

What am I missing?

[UPDATE - ANSWER]
Here's the code that worked for me:

string credentials = "username:password";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(formattedUrl);
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials)));
request.PreAuthenticate = true;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

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

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

发布评论

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

评论(2

奢欲 2024-12-29 18:56:59

首先,创建您的 creds 类:

NetworkCredential myCred = new NetworkCredential("username", "password");

将其添加到您的 Creds Cache:

CredentialCache credsCache = new CredentialCache();

// NOTE! THE PROTOCOL is needed in Uri (e.g. http://)
credsCache.Add(new Uri("proto://www.foo.com"), "Basic", myCred);

现在您可以执行 Web 请求:

WebRequest wr = WebRequest.Create("www.foo.com");

然后将 wr.Credentials 设置为 credsCache:

wr.Credentials = credsCache;

First you create your creds class:

NetworkCredential myCred = new NetworkCredential("username", "password");

Add it your Creds Cache:

CredentialCache credsCache = new CredentialCache();

// NOTE! THE PROTOCOL is needed in Uri (e.g. http://)
credsCache.Add(new Uri("proto://www.foo.com"), "Basic", myCred);

Now you can do a web request:

WebRequest wr = WebRequest.Create("www.foo.com");

Then set the wr.Credentials to the credsCache:

wr.Credentials = credsCache;
半仙 2024-12-29 18:56:59

纯文本密码不再是 URL 的有效部分(根据相应的 RFC)。正如您已经尝试过的那样,您应该使用NetworkCredentials。请参阅 MSDN 获取代码片段/示例

NetworkCredential myCred = new NetworkCredential(
    SecurelyStoredUserName,SecurelyStoredPassword,SecurelyStoredDomain);

CredentialCache myCache = new CredentialCache();

myCache.Add(new Uri("http://www.contoso.com"), "Basic", myCred);
myCache.Add(new Uri("http://app.contoso.com"), "Basic", myCred);

WebRequest wr = WebRequest.Create("http://www.contoso.com");
wr.Credentials = myCache;

UPD。与 MSDN 代码片段相比,需要在字符串前加上“http://”前缀,以避免出现“Invalid URI”异常。

Plain text password is no longer a valid part of URL (according to respective RFC). You should be using NetworkCredentials as you already tried. See MSDN for code snippet/sample:

NetworkCredential myCred = new NetworkCredential(
    SecurelyStoredUserName,SecurelyStoredPassword,SecurelyStoredDomain);

CredentialCache myCache = new CredentialCache();

myCache.Add(new Uri("http://www.contoso.com"), "Basic", myCred);
myCache.Add(new Uri("http://app.contoso.com"), "Basic", myCred);

WebRequest wr = WebRequest.Create("http://www.contoso.com");
wr.Credentials = myCache;

UPD. Compared to MSDN code snippet, one needs to prefix strings with "http://" to avoid "Invalid URI" exception.

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