前言
HttpClient在Web调用中具有广泛的应用,而为它添加默认请求头是我们经常遇到的需求,本文介绍4种为HttpClient添加默认请求头的方式。下面话不多说了,来一起看看详细的介绍吧
第一种方式
直接在创建的HttpClient对象的DefaultRequestHeaders集合中添加报头。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class Program
{
static Task Main()=> SendAsync1();
private static async Task SendAsync1()
{
var httpClient = new HttpClient();
AddDefaultHeaders(httpClient);
}
private static void AddDefaultHeaders(HttpClient httpClient)
{
httpClient.DefaultRequestHeaders.Add( "x-www-foo" , "123" );
httpClient.DefaultRequestHeaders.Add( "x-www-bar" , "456" );
httpClient.DefaultRequestHeaders.Add( "x-www-baz" , "789" );
}
}
|
第二种方式
对于.NET Core应用来说,我们更推荐的做法是采用依赖注入的方式,利用IHttpClientFactory来创建HttpClient对象,那么我们在进行相关服务注册的时候就可以设置默认请求报头。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class Program
{
static Task Main()=> SendAsync2();
private static async Task SendAsync2()
{
var services = new ServiceCollection();
services.AddHttpClient( "" , AddDefaultHeaders);
var httpClient = services
.BuildServiceProvider()
.GetRequiredService<IHttpClientFactory>()
.CreateClient();
}
private static void AddDefaultHeaders(HttpClient httpClient)
{
httpClient.DefaultRequestHeaders.Add( "x-www-foo" , "123" );
httpClient.DefaultRequestHeaders.Add( "x-www-bar" , "456" );
httpClient.DefaultRequestHeaders.Add( "x-www-baz" , "789" );
}
}
|
第三种方式
由于HttpClient在发送请求的时候会利用DiagnosticSource对象发送相应的诊断事件,并且将作为请求的HttpRequestMessage对象作为请求事件内容负载。我们可以订阅该事件,在请求被发送之前将其拦截下来,并添加相应的请求头即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
class Program
{
static Task Main()=> SendAsync3();
private static async Task SendAsync3()
{
Func< object , HttpRequestMessage> requestAccessor = null ;
DiagnosticListener.AllListeners.Subscribe(listener =>
{
if (listener.Name == "HttpHandlerDiagnosticListener" )
{
listener.Subscribe(kv =>
{
if (kv.Key == "System.Net.Http.HttpRequestOut.Start" )
{
requestAccessor ??= BuildRequestAccessor(kv.Value.GetType());
var request = requestAccessor(kv.Value);
AddDefaultHeaders(request);
}
});
}
});
var httpClient = new HttpClient();
static Func< object , HttpRequestMessage> BuildRequestAccessor(Type payloadType)
{
var property = payloadType.GetProperty( "Request" , BindingFlags.Instance | BindingFlags.Public);
var payload = Expression.Parameter( typeof ( object ));
var convertedPayload = Expression.Convert(payload, payloadType);
var getRequest = Expression.Call(convertedPayload, property.GetMethod);
var convert = Expression.Convert(getRequest, typeof (HttpRequestMessage));
return Expression.Lambda<Func< object , HttpRequestMessage>>(convert, payload).Compile();
}
}
private static void AddDefaultHeaders(HttpRequestMessage request)
{
request.Headers.Add( "x-www-foo" , "123" );
request.Headers.Add( "x-www-bar" , "456" );
request.Headers.Add( "x-www-baz" , "789" );
}
}
|
第四种方式
上面这种方式可以采用强类型编程方式,具体的代码如下。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
class Program
{
static Task Main()=> SendAsync4();
private static async Task SendAsync4()
{
DiagnosticListener.AllListeners.Subscribe(listener =>
{
if (listener.Name == "HttpHandlerDiagnosticListener" )
{
listener.SubscribeWithAdapter( new HttpClientListener());
}
});
var httpClient = new HttpClient();
}
private sealed class HttpClientListener
{
[DiagnosticName( "System.Net.Http.HttpRequestOut.Start" )]
public void OnSend(HttpRequestMessage request) => AddDefaultHeaders(request);
//Must subscribute the System.Net.Http.HttpRequestOut event.
[DiagnosticName( "System.Net.Http.HttpRequestOut" )]
public void OnSend() { }
}
private static void AddDefaultHeaders(HttpRequestMessage request)
{
request.Headers.Add( "x-www-foo" , "123" );
request.Headers.Add( "x-www-bar" , "456" );
request.Headers.Add( "x-www-baz" , "789" );
}
}
|
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。
原文链接:https://www.cnblogs.com/artech/p/add-headers-for-httpclient.html