简体   繁体   English

HTTP 请求标头自定义值不会发送到服务器 C#

[英]HTTP request header custom value not going to server C#

I am adding a custom header to my request.我正在向我的请求添加自定义标头。 I can see it when I debug the code.我调试代码时可以看到它。 But get an error that it's missing from the server.但是得到一个错误,它从服务器中丢失。

HttpWebRequest request;
request = GenerateRequest("https://at.test.com/test/api/test/v1/TestApplications HTTP/1.1", content, "application/json", "POST");

request.Headers.Add("Custom-Signature", signature);
return GetResponseContent(request);

That is how I'm adding my header.这就是我添加标题的方式。

Long time ago since this question.很久以前提出这个问题。 I had the same problem using web api 2.0 and my solution was to use Request.Content.Headers instead of Request.Headers我在使用 web api 2.0 时遇到了同样的问题,我的解决方案是使用Request.Content.Headers而不是Request.Headers

and then i declared an extestion as below然后我宣布了一个extestion如下

  /// <summary>
    /// Returns an individual HTTP Header value
    /// </summary>
    /// <param name="headers"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public static string GetHeader(this HttpContentHeaders headers, string key, string defaultValue)
    {
        IEnumerable<string> keys = null;
        if (!headers.TryGetValues(key, out keys))
            return defaultValue;

        return keys.First();
    }

And then i invoked it by this way.然后我通过这种方式调用它。

  var headerValue = Request.Content.Headers.GetHeader("custom-header-key", "default-value");

I hope it might be helpful我希望它可能会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM