已经制订,未免有HttpClient的用于构建REST客户端。但我想不通,也没有找到如何对服务器进行身份验证的任何实例。很可能我会使用基本的引渡,但实际上任何例子是AP preciated。
Been elaborating a bit with HttpClient for building a rest client. But I can't figure out, nor find any examples on how to authenticate towards the server. Most likely I will use basic aut, but really any example would be appreciated.
在早期版本(其中有例子在线)你这样做:
In earlier versions (which has examples online) you did:
HttpClient client = new HttpClient("http://localhost:8080/ProductService/");
client.TransportSettings.Credentials =
new System.Net.NetworkCredential("admin", "admin");
然而 TransportSettings
属性不再在0.3.0版本存在。
However the TransportSettings
property no longer exists in version 0.3.0.
所有这些都过时了。最后的办法做到这一点如下:
All these are out of date. The final way to do it is as follows:
var credentials = new NetworkCredential(userName, password);
var handler = new HttpClientHandler { Credentials = credentials };
using (var http = new HttpClient(handler))
{
// ...
}