如何使用基于HttpClient和.net4的Rest-client进行身份验证
一直在详细阐述 HttpClient 来构建休息客户端。但我无法弄清楚,也找不到任何有关如何向服务器进行身份验证的示例。我很可能会使用 basic aut,但实际上任何示例都会受到赞赏。
在早期版本(有在线示例)中,您是这样做的:
HttpClient client = new HttpClient("http://localhost:8080/ProductService/");
client.TransportSettings.Credentials =
new System.Net.NetworkCredential("admin", "admin");
但是 TransportSettings
属性在 0.3.0 版本中不再存在。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
所有这些都已经过时了。最终的实现方式如下:
All these are out of date. The final way to do it is as follows:
HttpClient 库没有进入 .Net 4。但是可以在此处使用 http://nuget.org/列表/包/HttpClient。但是,此版本的 HttpClient 中的身份验证方式有所不同。
或者
请注意,该库将于下周进行更新,并且有一些小的重大更改!
The HttpClient library did not make it into .Net 4. However it is available here http://nuget.org/List/Packages/HttpClient. However, authentication is done differently in this version of HttpClient.
or
And be warned, this library is going to get updated next week and there are minor breaking changes!
我尝试了邓肯的建议,但对我来说不起作用。我怀疑这是因为我正在集成的服务器没有发送质询或请求身份验证。它只是拒绝了我的请求,因为我没有提供授权标头。
因此,我执行了以下操作:
请注意,此处的示例仅适用于基本身份验证。
I tried Duncan's suggestion, but it didn't work in my case. I suspect it was because the server I was integrating with, didn't send a challenge or ask for authentication. It just refused my requests, because I didn't supply an Authorization header.
So I instead did the following:
Notice that the example here only works with Basic authentication.
从 .NET 4.8、.NET core 3.1 和 .NET Standard 2.0 开始,他们建议同时使用
HttpClient
和CredentialCache
。像这样的东西:编辑:请注意,将
HttpClient
包装到using
可能会导致高负载下的套接字耗尽。然而,将HttpClient
保持为单例可能会导致其他问题,例如陈旧的 DNS 问题。从此处开始阅读这个长篇故事。As of .NET 4.8, .NET core 3.1 and .NET Standard 2.0 they recommend using both
HttpClient
andCredentialCache
. Something like this:EDIT: Note that wrapping
HttpClient
intousing
may lead to Socket exhaustion under high load. However keepingHttpClient
as singleton may lead to other problems such as stale DNS issue. Start reading this long story from here.就其价值而言,使用 HttpClientHandler 没有任何作用,至少对于尝试对需要服务器管理凭据的 CouchDB API 进行身份验证的调用无效。
这对我有用:
正如此处答案中所述:
如何使用凭据在 C# 中的 HttpClient 中?
For what it is worth, nothing using HttpClientHandler worked, at least not for trying to make an authenticated call to the CouchDB API that requires server admin credentials.
This worked for me:
As outlined in the answer here:
How to use credentials in HttpClient in c#?
我相信这有点旧,但对于任何寻找更新答案的人来说,我在构建测试服务器时使用了以下代码:
或者这也有效:
I believe this is a bit old, but for anyone looking for an updated answer, I used this code when I built my test server:
Or this works too:
我刚刚下载了0.3.0,它确实被删除了。它现在位于
HttpClientChannel
上。如果未明确指定,它将使用
HttpClientChannel
的默认实例。更新:这现在对 .Net 4.5 无效;请参阅下面的正确答案:https://stackoverflow.com/a/15034995/58391
I just downloaded 0.3.0 it has indeed be removed. It's now on
HttpClientChannel
If not explicitly specified it uses a default instance of
HttpClientChannel
.UPDATE: this is now invalid for .Net 4.5; see correct answer below: https://stackoverflow.com/a/15034995/58391
它仅在指定
DefaultRequestHeaders
时有效。它没有任何其他方式工作。It only works when
DefaultRequestHeaders
is specified. It doesnt work any other way.