我正在尝试验证 API 响应,但我似乎无法理解如何使用响应的内容。
这是我的回应:
"{\"On\": false, \"value\": null,}"
我想测试“On”的值(如果它是真的那么......或者假那么......)。
到目前为止,这是我的代码:
using System;
using System.Net.Http;
using System.Collections.Generic;
using System.Net.Http.Headers;
namespace APITest
{
class Program
{
static void Main(string[] args)
{
PostRequest("My API");
Console.ReadKey();
}
async static void PostRequest(string url)
{
IEnumerable<KeyValuePair<string, string>> queries = new
List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("Test","1")
};
HttpContent q = new FormUrlEncodedContent(queries);
using (HttpClient client = new HttpClient())
{
using (HttpResponseMessage response = await
client.PostAsync(url,q))
{
using (HttpContent content = response.Content)
{
string mycontent = await
content.ReadAsStringAsync();
HttpContentHeaders headers = content.Headers;
Console.WriteLine(mycontent);
}
}
}
}
}
}