【发布时间】:2021-04-24 16:49:32
【问题描述】:
我需要使用 cutt.ly URL 较短的 API,我遵循了它的文档以及我使用它的方式
class Program
{
private const string URL = "https://cutt.ly/api/api.php";
private static string urlParameters = "?key=ddbbdd323230fbf3e0b9&short=https://www.google.com&name=Test";
static void Main(string[] args)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(URL);
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync(urlParameters).Result;.
if (response.IsSuccessStatusCode)
{
var dataObjects = response.Content.ReadAsAsync().Result;
foreach (var d in dataObjects)
{
Console.WriteLine("{0}", d.ToString());
}
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
client.Dispose();
Console.ReadLine();
}
}
但问题是我得到以下编译错误。
'HttpContent' does not contain a definition for 'ReadAsAsync' and no accessible extension method 'ReadAsAsync' accepting a first argument of type 'HttpContent' could be found
我正在使用 .net 核心。我如何使用 .net 核心来处理这个问题。我找到了这个question。但我不清楚它的答案。
【问题讨论】:
-
that question 中的答案可能与这个问题的答案相同。要么拉入 nuget 包,要么创建自己的扩展方法来反序列化 System.Text.Json
-
这与您的问题没有直接关系,您可能已经知道 - 但您可以让您的
Main返回async Task,这将允许您等待示例中的异步调用 - @ 987654324@ -
@devNull 你能帮帮我吗,我怎样才能为我的问题应用这个答案?
标签: c# json .net-core httpclient