使用HttpClient消费odata web api

编程入门 行业动态 更新时间:2024-10-25 04:15:51
本文介绍了使用HttpClient消费odata web api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嘿伙计们! 我开发了一个支持odata的ASP.NET Web Api(2.2)。 API运行正常,我用fiddler来发布,发布东西,api处理好一切。 现在我正在尝试编写一个消耗API的C#库。现在我曾经有一个没有odata支持的API,下面的代码工作正常:

Hey guys! I developed a ASP.NET Web Api (2.2) with odata support. The API works fine, I use fiddler to get, post stuff and the api handles everything fine. Now I'm trying to write a C# library that consumes the API. Now I used to have a API without odata support and the following code works just fine :

var url = "Support/Tickets"; HttpResponseMessage response = client.GetAsync(url).Result; if (response.IsSuccessStatusCode) { var list = response.Content.ReadAsAsync<List<Tickets>>).Result; }

(此处客户端是System.Net.Http.HttpClient对象) 但是,由于我实现了odata解决方案,因此从API返回的JSON发生了变化:

(client here is a System.Net.Http.HttpClient object) However, since I implemented the odata solution, the JSON returned from the API is changed :

{ "odata.metadata":"url.to/Support/$metadata#Tickets","value":[ { } ] }

现在粗线是新的并且导致线 var list = response.Content.ReadAsAsync< List< Tickets>>)。 失败。我明白这条线路失败的事实,但我无法弄清楚正确处理新JSON响应的方法。我需要使用某种odata解串器吗? 谢谢! Eduard

Now the bold line is new and causes the line var list = response.Content.ReadAsAsync<List<Tickets>>).Result; to fail. The fact that the line fails I understand, but I can't figure out what way to handle the new JSON response correctly. Do I need to use some kind of odata deserializer or something? Thanks! Eduard

推荐答案

元数据#票证, value:[ { } ] } metadata#Tickets","value":[ { } ] }

现在粗线是新的并导致线 var list = response.Content.ReadAsAsync< List< Tickets>>)。结果; 失败。我理解这条线路失败的事实,但我无法弄清楚正确处理新JSON响应的方法。我是否需要使用某种odata解串器或什么? 谢谢! Eduard

Now the bold line is new and causes the line var list = response.Content.ReadAsAsync<List<Tickets>>).Result; to fail. The fact that the line fails I understand, but I can't figure out what way to handle the new JSON response correctly. Do I need to use some kind of odata deserializer or something? Thanks! Eduard

更多推荐

使用HttpClient消费odata web api

本文发布于:2023-10-12 01:24:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:odata   HttpClient   api   web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!