如何在 c# 中的 HttpClient 中使用凭据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18478528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use credentials in HttpClient in c#?
提问by MikePR
I am facing some problems when using the HttpClient class to access to a Delicious API. I have the following code:
在使用 HttpClient 类访问 Delicious API 时,我遇到了一些问题。我有以下代码:
try
{
const string uriSources = "https://api.del.icio.us/v1/tags/bundles/all?private={myKey}";
using (var handler = new HttpClientHandler { Credentials = new
NetworkCredential("MyUSER", "MyPASS") })
{
using (var client = new HttpClient(handler))
{
var result = await client.GetStringAsync(uriSources);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ERROR...", MessageBoxButton.OK);
}
When running the code above I am getting the following: Response status code does not indicate success: 401 (Unauthorized).
运行上面的代码时,我得到以下信息:响应状态代码不表示成功:401(未经授权)。
So, how could I get this work? Is it possible?
那么,我怎样才能得到这份工作呢?是否可以?
Thanks in advance
提前致谢
Regards!
问候!
回答by Darrel Miller
The code you are showing works for me against an authenticated resource. I suspect Delicious is doing something weird.
您显示的代码适用于经过身份验证的资源。我怀疑 Delicious 正在做一些奇怪的事情。
Considering you are on Windows Phone, it is a pain to debug with Fiddler, so what I suggest is getting a Runscopeaccount. Install this message handlerwhich will redirect your request via the RunScope debugger. Once you do this, I suggest you look at the www-authenticate header and examine what that is returning.
考虑到您使用的是 Windows Phone,使用 Fiddler 进行调试很痛苦,因此我建议您获取一个Runscope帐户。安装此消息处理程序,它将通过 RunScope 调试器重定向您的请求。完成此操作后,我建议您查看 www-authenticate 标头并检查返回的内容。
If all else fails you can always set the Authentication header directly with basic auth credentials. You don't need to use the Credentials class.
如果所有其他方法都失败了,您始终可以使用基本身份验证凭据直接设置身份验证标头。您不需要使用 Credentials 类。
回答by Adam Szabo
I had the exact same problem myself. It seems the HttpClient
just disregards the credentials set in the HttpClientHandler
.
我自己也遇到了完全相同的问题。似乎HttpClient
只是无视在HttpClientHandler
.
The following shall work however:
但是,以下内容将起作用:
using System.Net.Http.Headers; // For AuthenticationHeaderValue
const string uri = "https://example.com/path?params=1";
using (var client = new HttpClient()) {
var byteArray = Encoding.ASCII.GetBytes("MyUSER:MyPASS");
var header = new AuthenticationHeaderValue(
"Basic", Convert.ToBase64String(byteArray));
client.DefaultRequestHeaders.Authorization = header;
var result = await client.GetStringAsync(uri);
}
No need for the handler.
不需要处理程序。
Source: http://www.snip2code.com/Snippet/13895/Simple-C---NET-4-5-HTTPClient-Request-Us
来源:http: //www.snip2code.com/Snippet/13895/Simple-C---NET-4-5-HTTPClient-Request-Us
回答by Nirav Darji
This is an old post but thought to add my reply for someone facing similar issue and browsing answers...
这是一篇旧帖子,但我想为面临类似问题和浏览答案的人添加我的回复...
I faced similar issue. In my case, setting Domain property for NetworkCredentials worked. You can try setting Domain.
我遇到了类似的问题。就我而言,为 NetworkCredentials 设置域属性有效。您可以尝试设置域。