如何在 .core 中使用“ReadAsAsync”
How to use “ReadAsAsync” in .core
我需要使用 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 核心来处理这个问题。我找到了这个 。但我不清楚它的答案。
如果您只需要一个短链接
var json = response.Content.ReadAsStringAsync().Result;
var result=JObject.Parse(json);
var shortLink = result["url"]["shortLink"].ToString();
我之前做过这个 here
您将必须安装 Newtonsoft.Json nuget 包
CuttlyApiKey 是您的 api 密钥,SelectedCustomText 是您 link 的自定义名称,如果您不想设置自定义名称,您可以设置 string.empty
public async Task<string> CuttlyShorten(string longUrl)
{
try
{
string url = string.Format("https://cutt.ly/api/api.php?key={0}&short={1}&name={2}", CuttlyApiKey, HttpUtility.UrlEncode(longUrl), SelectedCustomText);
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync(url))
using (HttpContent content = response.Content)
{
dynamic root = JsonConvert.DeserializeObject<dynamic>(response.Content.ReadAsStringAsync().Result);
string statusCode = root.url.status;
if (statusCode.Equals("7"))
{
string link = root.url.shortLink;
return link;
}
else
{
string error = root.status;
}
}
}
catch (Exception ex)
{
}
return "error";
}
用法:
var shortUrl = await CuttlyShorten(url);
我需要使用 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 核心来处理这个问题。我找到了这个
如果您只需要一个短链接
var json = response.Content.ReadAsStringAsync().Result;
var result=JObject.Parse(json);
var shortLink = result["url"]["shortLink"].ToString();
我之前做过这个 here 您将必须安装 Newtonsoft.Json nuget 包
CuttlyApiKey 是您的 api 密钥,SelectedCustomText 是您 link 的自定义名称,如果您不想设置自定义名称,您可以设置 string.empty
public async Task<string> CuttlyShorten(string longUrl)
{
try
{
string url = string.Format("https://cutt.ly/api/api.php?key={0}&short={1}&name={2}", CuttlyApiKey, HttpUtility.UrlEncode(longUrl), SelectedCustomText);
using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync(url))
using (HttpContent content = response.Content)
{
dynamic root = JsonConvert.DeserializeObject<dynamic>(response.Content.ReadAsStringAsync().Result);
string statusCode = root.url.status;
if (statusCode.Equals("7"))
{
string link = root.url.shortLink;
return link;
}
else
{
string error = root.status;
}
}
}
catch (Exception ex)
{
}
return "error";
}
用法:
var shortUrl = await CuttlyShorten(url);