无法从带有c#的API获得json响应

时间:2022-10-08 08:57:33

I am just learning how to work with an API. I have been able to request and get a security token and I am able to get authenticated to the API for a GET request to return the information I want. I use the code shown below to make my GET request.

我只是在学习如何使用API。我已经能够请求并获得一个安全令牌,并且我能够通过一个get请求的API进行身份验证以返回我想要的信息。我使用下面显示的代码来完成我的GET请求。

client.BaseAddress = new Uri("https://api.example.com");
client.DefaultRequestHeaders.Accept.Clear();
//client.DefaultRequestHeaders.Accept.Add(new
MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization" ,"Bearer "+ accessToken);
client.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
HttpResponseMessage response =client.GetAsync"/api/v1/agencies/agencyId/users?email=Test.Test@Test.net").Result;
if (response.IsSuccessStatusCode)
{
    string json = "Need help here";
}

I get authenticated to the API but just can not figure out how to get the json data. I have found several examples that use a command prompt application with a local API; they all seem to use async with wait, but those do not seem to work in an ASP.Net web application.

我得到了API的认证,但就是不知道如何获得json数据。我发现了几个使用命令提示应用程序和本地API的示例;它们似乎都使用异步和等待,但它们似乎并不在ASP中工作。净的web应用程序。

Can anyone help me out or show an example that is using a web form?

有谁能帮助我或者展示一个使用web表单的例子吗?

1 个解决方案

#1


1  

You can get the json like that:

可以得到json:

if (response.IsSuccessStatusCode)
{
    string json = response.Content.ReadAsStringAsync().Result;
}

#1


1  

You can get the json like that:

可以得到json:

if (response.IsSuccessStatusCode)
{
    string json = response.Content.ReadAsStringAsync().Result;
}