【问题标题】:How to read my custom Flurl headers from HttpCall如何从 HttpCall 读取我的自定义 Flurl 标头
【发布时间】:2019-09-25 13:13:08
【问题描述】:

如何阅读我放在FlurlRequestWithHeader(string name, object value)AuthorizationWithOAuthBearerToken(string token) 上的自定义标头从HttpCall FlurlClient 设置给我们Func<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 标头。

【问题讨论】:

    标签: c# rest logging flurl self-signed-certificate


    【解决方案1】:

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

    httpCall.Request.Content?.Headers
    

    使用这个:

    httpCall.FlurlRequest.Headers
    

    【讨论】:

    • 我尝试过一次,但现在我意识到我尝试了没有自定义标题的呼叫:P
    【解决方案2】:

    为了从 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"。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-01
      • 2012-09-08
      • 2020-08-19
      • 1970-01-01
      • 2021-10-06
      相关资源
      最近更新 更多