C# Microsoft.Data.OData.ODataContentTypeException:找不到与响应的内容类型匹配的受支持MIME类型
我遇到了以下异常C# Microsoft.Data.OData.ODataContentTypeException:找不到与响应的内容类型匹配的受支持MIME类型,c#,.net,sharepoint,rest,odata,C#,.net,Sharepoint,Rest,Odata,我遇到了以下异常 Microsoft.Data.OData.ODataContentTypeException: A supported MIME type could not be found that matches the content type of the response. None of the supported type(s) 'application/atom+xml;type=entry, application/atom+xml, application/json
Microsoft.Data.OData.ODataContentTypeException:
A supported MIME type could not be found that matches the content
type of the response. None of the supported type(s)
'application/atom+xml;type=entry, application/atom+xml, application/json;odata=verbose'
matches the content type 'application/json'.
at Microsoft.Data.OData.MediaTypeUtils.GetFormatFromContentType....
使用代码
var someJason = "{'__metadata':{'type':'SP.Data.TestObjectItem'}, " + jsonProperties + "}";
var content = new ByteArrayContent(Encoding.UTF8.GetBytes(someJason));
content.Headers.Add("X-RequestDigest", formDigest);
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, queryUrl);
httpRequestMessage.Content = content;
httpRequestMessage.Headers.Add(HttpRequestHeader.Accept.ToString(),
"application/json;odata=verbose");
httpRequestMessage.Headers.Add(HttpRequestHeader.ContentType.ToString(),
"application/json;odata=verbose");
var result = await client.SendAsync(httpRequestMessage);
result
变量始终返回400“错误请求”。我找不到任何头值,这些头值将强制此代码使用允许的内容类型。在某些情况下,当我在Accept头中设置一些总垃圾时,我得到了406个完全符合文档要求的“不可接受”
根据这个odata文档,我还试图将查询字符串设置为$format=JSON
和$format=atom
,但我从未注意到这个参数有任何不同(似乎被忽略了)
所以我的问题是-我缺少什么
WinRT应用程序调用SharePoint 2013 OData服务(_api/web/lists/)时使用了该代码,但我认为(并希望)问题并没有那么具体。您能否尝试捕获HTTP跟踪(Fiddler肯定可以做到这一点)。从代码上看,它看起来不错,但重要的是线路上的内容。