当我在不等待的情况下使用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();
}
}
然后我在下面的Visual Studio图像上出现错误。
如果我不想等待响应,我该怎么办?
答案 0 :(得分:-1)
我认为问题是异步等待的基本概念,请查看此链接以了解更清晰的想法:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/