[英]Call async method HttpClient without await throw error
当我在没有等待的情况下使用 httpclient 调用异步方法时,我不想等待响应然后抛出异常“对象引用未设置为对象的实例”
\\ Call PostAsync
var ignore = webRequest.PostAsync(json, token);
\\ PostAsync method
using (var httpClient = new HttpClient())
{
if (!string.IsNullOrEmpty(auth))
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", auth);
var content = new StringContent(json, Encoding.UTF8, "application/json");
// this line throw exception
var response = await httpClient.PostAsync(this.URL, content);
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsStringAsync();
}
}
然后我在下面的视觉工作室图像上出现错误。
如果我不想等待回复,我该怎么做?
我认为问题是异步等待的基本概念,请查看此链接以获得更清晰的想法:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.