http客户端帖子ynnc总是在Web API项目中取消任务,而该任务在Console App c#中工作。
我创建了两个项目,一个是控制台应用程序另一个是C#中的Web API,在此中,我使用PostAsync方法使用HTTPCLIENT称为API。它在控制台应用程序中工作正常(.NET Framework 4.7.2)。在Web API(.NET Framework 4.7.2)中,始终在TaskCancell中使用相同的代码。 以下是代码片段。
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + data.token);
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "b75b570ad98148968beac9510ecaba6a");
client.DefaultRequestHeaders.Add("Ocp-Apim-Trace", "false");
client.DefaultRequestHeaders.TransferEncodingChunked = false;
var postTask = client.PostAsync(data.url, new StringContent(
new JavaScriptSerializer().Serialize(rAS), Encoding.UTF8, "application/json")).Result;
if (response.IsSuccessStatusCode)
{
jsonResponse = postTask.Content.ReadAsStringAsync().Result;
}
}
我得到的例外是 - “任务已取消。”。相同的代码正在使用控制台应用程序。这是非常令人沮丧的人,请帮忙。
I have created two projects, one is console application another is WEB API in c#, In this i have called a API using HTTPClient using postAsync method. It is working fine in Console Application (.net Framework 4.7.2). While in Web API (.net Framework 4.7.2) same code is always goes in TaskCanCelled .
Below is code snippet.
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + data.token);
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "b75b570ad98148968beac9510ecaba6a");
client.DefaultRequestHeaders.Add("Ocp-Apim-Trace", "false");
client.DefaultRequestHeaders.TransferEncodingChunked = false;
var postTask = client.PostAsync(data.url, new StringContent(
new JavaScriptSerializer().Serialize(rAS), Encoding.UTF8, "application/json")).Result;
if (response.IsSuccessStatusCode)
{
jsonResponse = postTask.Content.ReadAsStringAsync().Result;
}
}
Exception i got is something - "A task was canceled.". Same code is working on Console Application. This is very frustrating guys please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论