如何从 HttpCall 读取我的自定义 Flurl headers

How to read my custom Flurl headers from HttpCall

如何从 FlurlRequest throug WithHeader(string name, object value)Authorization throug WithOAuthBearerToken(string token) 中读取自定义 headers =16=] FlurlClient 设置给我们 Func<HttpCall, Task> BeforeCallAsync()?

我设计了我们的解决方案 FlurlClient 以动态设置 self-signed 证书和自定义 JSON 命名策略, 必须与第三个进行通信派对 API.

我正在使用 Flurl.Http 2.4.2.0

_flurlClient = new FlurlClient()
    .Configure(settings =>
    {
        settings.HttpClientFactory = new X509HttpFactory(
            _certificateConfiguration.PersonalInfoExchangePath,
            _certificateConfiguration.ClientSecret);
        settings.JsonSerializer = _thirdPartyApiJsonSerializer;
        settings.BeforeCallAsync = async (httpCall) =>
        {
            thirdPartyRequestIdentity = await _appThirdPartyRequestRepository.Insert(
                new ThirdPartyRequest
                {
                    AppRequestId = await _contextAccessor.GetRequestId(),
                    RawContent = JsonConvert.SerializeObject(
                        new
                        {
                            Method = httpCall.Request
                                .Method
                                .ToString(),
                            Content = httpCall.Request.Content != null
                                ? await httpCall.Request
                                    .Content
                                    .ReadAsStringAsync()
                                : null,
                            Headers = httpCall.Request
                                .Content?.Headers
                                .Where(h => h.Value != null)
                                .Select(h => new
                                {
                                    h.Key,
                                    Value = h.Value.ToArray()
                                })
                                .ToArray(),
                            httpCall.Request.RequestUri.AbsoluteUri,
                            httpCall.RequestBody,
                            EnvironmentHostname = await _environmentContext.GetHostname(),
                            EnvironmentAddressList = (await _environmentContext.GetAddressList())
                                .Select(a => a.ToString())
                        },
                        _recoveryJsonSerializerSettings),
                    Timestamp = DateTime.Now
                });
        };
    });

我需要记录所有 header,但是使用此代码,我得到的唯一 header 是 Content-Type header.

IFlurlRequest 而不是 HttpRequestMessage 获取它们。换句话说,而不是这个:

httpCall.Request.Content?.Headers

使用这个:

httpCall.FlurlRequest.Headers

为了从 Flurl 响应中读取 headers,我使用了这个

 IFlurlResponse response = await request.WithHeaders(
    new { Accept = "application/xml" }).GetAsync();
 IReadOnlyNameValueList<string> headers = response.Headers; 
 string contentType = headers.FirstOrDefault(h => h.Name == "Content-Type").Value;
 return await response.GetStreamAsync();

这给出了 contentType="image/png".