1

我如何阅读我放在我的FlurlRequestthroug上的自定义标题WithHeader(string name, object value)以及设置中的Authorizationthroug给我们?WithOAuthBearerToken(string token)HttpCall FlurlClientFunc<HttpCall, Task> BeforeCallAsync()

我设计了我们的解决方案,FlurlClient以动态设置自签名证书和自定义 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
                });
        };
    });

我需要记录所有标题,但使用此代码,我得到的唯一标题(如果适用)是Content-Type标题。

4

2 回答 2

1

从而IFlurlRequest不是从HttpRequestMessage. 换句话说,而不是这样:

httpCall.Request.Content?.Headers

用这个:

httpCall.FlurlRequest.Headers
于 2019-09-26T14:03:59.397 回答
1

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

 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"。

于 2020-10-29T07:46:31.957 回答