asp.net 任务已取消PostAsync C#

1l5u6lss  于 2022-11-19  发布在  .NET
关注(0)|答案(1)|浏览(329)
protected HttpResponseMessage SendJsonRequest(HttpClient client, string jsonRequest)
    {
        var mediaType = "application/json";
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(mediaType));

        return client.PostAsync(ApiUrl, new StringContent(jsonRequest, Encoding.UTF8, mediaType)).Result;
    }

错误是任务被取消,它是否与Api相关或将来自我的代码?

kzmpq1sx

kzmpq1sx1#

您没有等待PostAsync

protected async Task<HttpResponseMessage> SendJsonRequest(HttpClient client, string jsonRequest)
{
    var mediaType = "application/json";
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(mediaType));

    return await client.PostAsync(ApiUrl, new StringContent(jsonRequest, Encoding.UTF8, mediaType));
}

相关问题