c# - 如何从 HttpCall 读取我的自定义 Flurl header

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

如何读取我放在 FlurlRequest 上的自定义 header 通过WithHeader(string name, object value)Authorization通过WithOAuthBearerToken(string token)来自HttpCall FlurlClient设置给我们 Func<HttpCall, Task> BeforeCallAsync()

我用 FlurlClient 设计了我们的解决方案要动态设置自签名证书和自定义 JSON 命名策略,必须与此第三方 API 进行通信。

我正在使用 Flul.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 。

最佳答案

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

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

关于c# - 如何从 HttpCall 读取我的自定义 Flurl header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58099278/

相关文章:

c# - .NET 中 ","(逗号)和 "."(点)的 KeyCode 是什么?

node.js - 使用 nodejs 创建 REST API 以与 contentBlocks 一起使用 (createjs.org)

Python logging.DEBUG 级别不记录

python - 如何在 python 的 logging.conf 中导入 ssl 模块?

javascript - 解释在 winston logger 中级别的使用

c# - 将标准 C++ 字符串编码为 C# 字符串时出现问题

c# - 如何将文件上传到blob容器?

c# - 在 C# 中使用 WIA 生成照片质量扫描

javascript - 通过 Blogger API v3.0 为帖子插入评论

api - REST API框架。无效的querystring参数的建议行为