如何为DeleteAsync设置标头

时间:2015-05-15 00:44:20

标签: c# async-await dotnet-httpclient

我正在尝试在HttpClient.DeleteAsync请求上设置自定义标头。我尝试过使用

document.getElementById("web").innerHTML = favWebComics[x];

但是会收到此错误

httpClient.DefaultRequestHeaders.Add("X-Parse-Application-Id",ParseAppID);

HttpClient.SendAsync可以使用

发送自定义请求标头
Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.

和HttpClient.PostAsync可以用

发送它们
System.Net.Http.HttpRequestMessage.Headers.Add("X-Parse-Application-Id",ParseAppID);

如何使用DeleteAsync执行此操作?

1 个答案:

答案 0 :(得分:3)

实例化HttpRequestMessage并改为使用SendAsync

var request = new HttpRequestMessage(HttpMethod.Delete, requestUri);
request.Headers.Add("X-Parse-Application-Id", ParseAppID);

using (var response = await _calendarClient.SendAsync(request).ConfigureAwait(false))
{
    if (response.StatusCode.Equals(HttpStatusCode.NotFound))
    {
        return;
    }
    response.EnsureSuccessStatusCode();
}