【问题标题】:c# httpcontent - add Header If-Match errorsc# httpcontent - 添加 Header If-Match 错误
【发布时间】:2019-10-21 03:24:54
【问题描述】:

添加新标题“If-Match”

 using (HttpContent content = new StringContent(serializedObject))
        {
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            content.Headers.Remove("If-Match");
            content.Headers.Add("If-Match", "XXXXXXXXXX");
        }

抛出:

Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.

我可以添加任何其他标题

【问题讨论】:

  • 您正在设置响应标头,但使用的是请求标头保留字?
  • 使用的 api 需要将 If-Match 放在 PUT 的标头中
  • 好的,不要在内容上设置它,而是在父级上设置它

标签: httpcontent


【解决方案1】:

编辑:

using(var request = new HttpRequestMessage(HttpMethod.Put, new Uri(url))) {
    request.Headers.Remove("If-Match");
    request.Headers.TryAddWithoutValidation("If-Match", "XXXXXXXXXX");
    using (HttpContent content = new StringContent(serializedObject))
    {
        content.Headers.Remove("Content-Type");
        content.Headers.Add("Content-Type", "application/json");
    }
    // ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-07
    • 2014-06-27
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 2013-07-18
    相关资源
    最近更新 更多