30 lines
945 B
C#
30 lines
945 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CloudGaming.Core.AgileConfig
|
|
{
|
|
public class AuthenticatedHttpClientHandler : DelegatingHandler
|
|
{
|
|
private readonly string _token;
|
|
|
|
public AuthenticatedHttpClientHandler(string token)
|
|
{
|
|
_token = token ?? throw new ArgumentNullException(nameof(token));
|
|
InnerHandler = new HttpClientHandler(); // 确保内部处理程序已分配
|
|
}
|
|
|
|
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
|
{
|
|
// 添加授权头
|
|
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _token);
|
|
|
|
// 调用基类实现
|
|
return await base.SendAsync(request, cancellationToken);
|
|
}
|
|
}
|
|
}
|