【发布时间】:2019-04-30 03:09:49
【问题描述】:
我正在尝试发送 POST 请求。 虽然通过 POSTMAN 发送一切顺利,然后我尝试通过 C# 代码发送:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
var client = new RestClient(MY-URL);
var request = new RestRequest(Method.POST);
request.Credentials = new System.Net.NetworkCredential(ServerUsername, Password);
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("undefined", My JSON Data, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
我收到此错误:
请求包含实体主体,但没有 Content-Type 标头。此资源不支持推断的媒体类型“application/octet-stream”
我该如何解决?
【问题讨论】:
-
您是在发送原始 JSON 还是在序列化要发送的对象模型?如果是后者,请将
request.AddParameter替换为request.AddJsonBody(model) -
否则添加参数时需要包含type即
request.AddParameter("application/json", My JSON Data, ParameterType.RequestBody);
标签: c# rest asp.net-web-api postman