C#httpcontent-添加标头If-Match错误

时间:2019-06-05 15:24:09

标签: httpcontent

添加新的标题“ 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.

我可以添加其他任何标头

1 个答案:

答案 0 :(得分: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");
    }
    // ...
}