使用HTTPClient调用WebAPI [英] Call WebAPI Using HTTPClient

查看:237
本文介绍了使用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.

推荐答案

您可以使用 https://github.com/chris-herring/DataTableConverter [ ^ ]将json转换为datatable像这样:

DataTable table = JsonConvert.DeserializeObject< datatable>(json ,新的Serialization.DataTableConverter());
You can use https://github.com/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆