【发布时间】:2014-11-05 04:07:52
【问题描述】:
为什么HttpMethods如GET和DELETE不能包含body?
public Task<HttpResponseMessage> GetAsync(Uri requestUri);
public Task<HttpResponseMessage> DeleteAsync(string requestUri);
同样在 Fiddler 中,如果我提供一个主体,背景会变为红色。但它仍然会在 body 上执行。
因此,作为替代方案,我使用了SendAsync(),因为它接受HttpRequestMessage,其中可以包含HttpMethod 以及内容。
// other codes
Category category = new Category(){ Description = "something" };
string categoryContent = JsonConvert.SerializeObject(category);
string type = "application/json";
HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Delete, "-page-")
HttpContent content = new StringContent(categoryContent, Encoding.UTF8, type);
HttpClient client = new HttpClient();
message.Content = content;
await client.SendAsync(message, HttpCompletionOption.ResponseHeadersRead);
// other codes
我错过了什么吗?
【问题讨论】:
-
您是否有任何特殊原因要发送带有 GET 或 DELETE 请求的正文?一般来说,没有理由在这些类型的请求中使用正文。
标签: c# asp.net-web-api