NET4与休息客户端进行身份验证

NET4与休息客户端进行身份验证

本文介绍了如何基于HttpClient的和.NET4与休息客户端进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经制订,未免有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))
{
    // ...
}

这篇关于如何基于HttpClient的和.NET4与休息客户端进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 11:19