wpf HttpClient PostAsync 和 SendAsync 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49142723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between HttpClient PostAsync and SendAsync
提问by Ian Moriarty
Working on a project where a WPF front end, and trying to get a handle on async calls to HttpClient and I've been going around and around trying to get PostAsync to work, but it routinely appears to deadlock, or at least the post response times out, even with large values for timeout, and a visible response in fiddler.
在一个 WPF 前端的项目上工作,并试图处理对 HttpClient 的异步调用,我一直在四处尝试让 PostAsync 工作,但它经常出现死锁,或者至少是发布响应超时,即使超时值很大,并且 fiddler 中的响应可见。
So, after a while I decided to try out a few other methods on HttpClient, and they worked, first try. No idea why.
所以,过了一会儿,我决定在 HttpClient 上尝试其他一些方法,它们奏效了,首先尝试。不知道为什么。
I'm clean all the way to my WPF button with awaits, asyncs, and .ConfigureAwait(false)(I think):
我是干净的一路我的WPF按钮awaits,asyncs和.ConfigureAwait(false)(我认为):
Button:
按钮:
private async void Generate_Suite_BTN_Click(object sender, RoutedEventArgs e)
{
await suiteBuilder.SendStarWs().ConfigureAwait(false);
}
XmlDoc Load:
XmlDoc 加载:
internal async Task SendStarWs()
{
var xmlDoc = new XmlDocument();
xmlDoc.Load("C:\Temp\file.xml");
await StarWSClient.SendStarMessage(xmlDoc).ConfigureAwait(false);
}
SendMessage:
发信息:
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);
}
This call '500s' against my endpoint immediately, which I'd expect:
这个调用 '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;
}
The Post variant returns a TaskCancellationException, with timeout message regardless of timeout value:
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;
}
My endpoint responds normally to our other software, so I'm pretty sure the endpoint is solid, I can't fathom why the post response is blocked, when the send isn't.
我的端点对我们的其他软件正常响应,所以我很确定端点是可靠的,我无法理解为什么在发送时阻止了发布响应。
回答by JSteward
SendAsynccan make any http verb request depending on how you set that property. PostAsyncand similar are just convenience methods. Those convenience methods use SendAsyncinternally which is why when you derive a handler you only need to override SendAsyncand not all of the send methods.
SendAsync可以根据您设置该属性的方式发出任何 http 动词请求。PostAsync和类似的只是方便的方法。这些方便的方法在SendAsync内部使用,这就是为什么当您派生处理程序时,您只需要覆盖SendAsync而不是所有的发送方法。
To your other question though:When you use SendAsyncyou need to create the content and pass it. Your only sending an empty message. The 500 likely means that the api got nullfrom the model binding and kicked you back. Just as @John commented.
不过,对于您的另一个问题:当您使用时,SendAsync您需要创建内容并传递它。您只发送一条空消息。500 可能意味着 apinull从模型绑定中获取并将您踢回去。正如@John 评论的那样。

![wpf 使用 Fody 时出错 [ImplementPropertyChanged]](/res/img/loading.gif)