使用HTTPClient调用WebAPI

编程入门 行业动态 更新时间:2024-10-27 09:43:48
本文介绍了使用HTTPClient调用WebAPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用httpclient调用web api,我希望能够将JObject绑定到gridview 这里是我的代码,无法正常工作。 />

i am calling a web api using httpclient, i want to be able to bind the JObject to a gridview here is my code that aint working.

protected void Button2_Click(object sender, EventArgs e) { HttpClient client = new HttpClient(); client.BaseAddress = new Uri(&"site name";); // Add an Accept header for JSON format. client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = client.GetAsync("user/view.json?api_public=M01&api_secret=1;api_name=ezd").Result; if (response.IsSuccessStatusCode) { // Parse the response body. Blocking! string matchIdToFind = "26"; var product = response.Content.ReadAsAsync<JObject>().Result; JObject match = product; GridView1.DataSource = match; GridView1.DataBind(); } else { TextBox3.Text = "errror"; } }

这里我得到了所需的响应,但无法将响应绑定到gridview,我们将非常感谢任何帮助或帮助。

Here i have gotten the required response, but could not bind the response to a gridview any help or assistance will be greatly appreciated thanks in advance.

推荐答案

您可以使用 github/chris-herring/DataTableConverter [ ^ ]将json转换为datatable像这样: DataTable table = JsonConvert.DeserializeObject< datatable>(json ,新的Serialization.DataTableConverter()); You can use github/chris-herring/DataTableConverter[^] to convert json to datatable Like this: DataTable table = JsonConvert.DeserializeObject<datatable>(json, new Serialization.DataTableConverter());

我发现文章解释 Json .NET - 反序列化DataTable [ ^ ]。 因此,转换为DataTable后,只需将其分配给 GridView1.DataSource 。 I found the article explaining Json .NET–Deserializing a DataTable[^]. So, after converting to DataTable, just assign that to GridView1.DataSource.

更多推荐

使用HTTPClient调用WebAPI

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

发布评论

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

>www.elefans.com

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