【问题标题】:Difference between HttpClient PostAsync and SendAsyncHttpClient PostAsync 和 SendAsync 之间的区别
【发布时间】:2018-03-07 01:30:26
【问题描述】:

在一个 WPF 前端的项目上工作,并试图处理对 HttpClient 的异步调用,我一直在四处走动,试图让 PostAsync 工作,但它通常会出现死锁,或者至少发布响应超时,即使超时值很大,并且在提琴手中有可见的响应。

所以,过了一会儿,我决定在 HttpClient 上尝试其他一些方法,它们都奏效了,首先尝试一下。不知道为什么。

我一直使用awaitsasyncs.ConfigureAwait(false)(我认为)来清理我的 WPF 按钮:

按钮:

private async void Generate_Suite_BTN_Click(object sender, RoutedEventArgs e)
{
    await suiteBuilder.SendStarWs().ConfigureAwait(false);
}

XmlDoc 加载:

internal async Task SendStarWs()
{
    var xmlDoc = new XmlDocument();
    xmlDoc.Load("C:\\Temp\\file.xml");
    await StarWSClient.SendStarMessage(xmlDoc).ConfigureAwait(false);
}

发送消息:

private static readonly HttpClient Client = new HttpClient {MaxResponseContentBufferSize = 1000000};

public static async Task<STARResult> SendMessage(vars)
{
var response = await SendRequestAsync(url, contentNew, Client).ConfigureAwait(false);
return new STARResult(response, hash);
}

这会立即针对我的端点调用“500s”,这是我所期望的:

var response = await SendRequestAsync(url, contentNew, Client).ConfigureAwait(false);

private static async Task<HttpResponseMessage> SendRequestAsync(string adaptiveUri, StringContent content, HttpClient httpClient)
{
    HttpResponseMessage responseMessage = null;
    try
    {
        responseMessage = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Post, adaptiveUri)).ConfigureAwait(false);
    }
    catch (Exception ex)
    {
        if (responseMessage == null)
            responseMessage = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.InternalServerError,
                ReasonPhrase = $"SendRequestAsync failed: {ex.Message}"
            };
    }
    return responseMessage;
}

Post 变体返回一个 TaskCancellationException,并带有超时消息,无论超时值如何:

var response = await PostRequestAsync(url, contentNew, Client).ConfigureAwait(false);

private static async Task<HttpResponseMessage> PostRequestAsync(string adaptiveUri, StringContent content, HttpClient httpClient)
{
    HttpResponseMessage responseMessage = null;
    try
    {
        responseMessage = await httpClient.PostAsync(adaptiveUri, content).ConfigureAwait(false);
    }
    catch (Exception ex)
    {
        if (responseMessage == null)
            responseMessage = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.InternalServerError,
                ReasonPhrase = $"PostRequestAsync failed: {ex.Message}"
            };
    }
    return responseMessage;
}

我的端点正常响应我们的其他软件,所以我很确定端点是可靠的,我无法理解为什么发送响应被阻止,而发送却没有。

【问题讨论】:

  • 在您使用SendAsync 时,您并未包含内容。在PostAsync 中包含内容。我认为这与您的问题比您使用的方法更相关。
  • 好点 - 我更改了发送到包含的内容,现在它具有相同的行为。现在要弄清楚为什么它会超时。

标签: c# wpf async-await dotnet-httpclient


【解决方案1】:

SendAsync 可以发出任何 http 动词请求,具体取决于您设置该属性的方式。 PostAsync 和类似的只是方便的方法。这些便捷方法在内部使用SendAsync,这就是为什么当您派生处理程序时您只需要覆盖SendAsync 而不是所有的发送方法。

关于你的其他问题: 当您使用SendAsync 时,您需要创建内容并传递它。您只发送一条空消息。 500 可能意味着 api 从模型绑定中获得了null 并把你踢了回来。正如@John 评论的那样。

【讨论】:

  • 真的很公平,我只是不清楚为什么一个被阻止(发布/超时),而另一个不是(立即发送/响应。)
  • 我不太明白你看到了什么行为,你能详细说明一下吗? TaskCancelledException 有内部异常吗? Feel free to browse the source here
  • 不,没有内部异常,而且 CancellationRequest= false,这就是我假设超时的原因。
  • @Ian 服务器是否对请求发送了很大的响应?也许您需要查看 HttpCompletionOptions 并在 POST 时等待标头。
猜你喜欢
  • 1970-01-01
  • 2012-11-15
  • 2020-08-14
  • 1970-01-01
  • 2020-08-07
  • 2019-11-17
  • 2021-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多