【发布时间】:2014-05-14 02:05:13
【问题描述】:
我是 web api 的新手,我正在编写一个代码,我在其中发送 json 数据,用于对 web api(web 服务)的 PUT/POST 请求。 我正在做以下事情
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:9000/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var gizmo = some json data;
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post,"url");
req.Content = new StringContent(some json data, Encoding.UTF8,"application/json"));
client.Timeout = TimeSpan.FromSeconds(500);
response = await client.PostAsJsonAsync("api/products", gizmo);
}
我的问题是我是否必须将内容类型的代码放在内容标题中,我观察到即使确实将内容类型作为“applicipn/json”专门用于我的代码中的内容类型 并在 Fiddler 中检查我的请求,它仍然显示内容类型:text/html。 为什么会这样?。 非常感谢您的所有回复
【问题讨论】:
标签: c# asp.net-web-api dotnet-httpclient