添加另一个 http 请求 header c#

To add another http request header c#

需要了解为现有的 http 请求添加 http header 的简单方法。

下面是我的中间件代码

   public class ProviderStateMiddleware
    {
        private ITestOutputHelper _outputHelper { get; }
        private const string ConsumerName = "test";
        private readonly RequestDelegate _next;
        private readonly IDictionary<string, Action> _providerStates;

        public ProviderStateMiddleware(RequestDelegate next)
        {
            _next = next;

            _providerStates = new Dictionary<string, Action>
            {
                {
                    "A session id",
                    getSessionID
                },
            };
        }

        private void getSessionID()
        {



        }

        public async Task Invoke(HttpContext context)
        {

            if (context.Request.Path.Value == "/provider-states")
            {
                this.HandleProviderStatesRequest(context);
                await context.Response.WriteAsync(String.Empty);
            }
            else
            {
                await this._next(context);
            }
        }

        private void HandleProviderStatesRequest(HttpContext context)
        {
            context.Response.StatusCode = (int)HttpStatusCode.OK;

            if (context.Request.Method.ToUpper() == 
    HttpMethod.Post.ToString().ToUpper() &&
                context.Request.Body != null)
            {
                string jsonRequestBody = String.Empty;
                using (var reader = new StreamReader(context.Request.Body, 
    Encoding.UTF8))
                {
                    jsonRequestBody = reader.ReadToEnd();

                }

                var providerState = JsonConvert.DeserializeObject<ProviderState> 
     (jsonRequestBody);

                //A null or empty provider state key must be handled
                if (providerState != null && 
       !String.IsNullOrEmpty(providerState.State) &&
                    providerState.Consumer == ConsumerName)
                {

                    _providerStates[providerState.State].Invoke();
                }
            }
        }
    }
    }

我是 c# 或 http 中间件部分的新手,请让我知道在哪里可以添加自定义 header,如下所示 json。我在这里看了一些帖子,但对我的理解并不多。

    {Subscriber-id : "1234"}

From MSDN:

HttpResponse.OnStarting

Adds a delegate to be invoked just before response headers will be sent to the client.

将此代码添加到您的 public async Task Invoke(HttpContext context) 函数中:

context.Response.OnStarting(() =>
        {
            context.Response.Headers.Add("Key", "Value");
            return Task.CompletedTask;
        });

这是使用 PactVerifierConfig.CustomHeader 方法处理的,该方法添加了请求 headers。

var config = new PactVerifierConfig
        {

            Outputters = new List<IOutput>
                            {
                                new XUnitOutput(_outputHelper)
                            },
            //Custom header
            CustomHeader = new KeyValuePair<string, string>("testId","test123"),
            // Output verbose verification logs to the test output
            Verbose = true
        };