我有一个需要在请求头中发送自定义身份验证令牌的WebAPI。
using (var client = new HttpClient()
{
client.BaseAddress = new Uri("https://example.com");
var request = new HttpRequestMessage(HttpMethod.Post, "/adfs/oauth2/authorize);
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "<token>");
...
...
//code removed for brevity
}
因为许多操作将调用这些精确的代码行。我计划创建一个HttpClient的实现类,如下所示。这个类的对象需要已经初始化了所有的自定义实现。这可以做到吗?
public class MyHttpClient : HttpClient
{
//how do I make sure the object of MyHttpClient will implicitly set all the desired properties (headers etc)
}
using (var client = new MyHttpClient()
{
//client should come with all the properties initialized
}
https://stackoverflow.com/questions/50751748
复制相似问题