HttpClient PostAsync 和 SendAsync 之间的区别
Difference between HttpClient PostAsync and SendAsync
在一个 WPF 前端的项目上工作,并试图处理对 HttpClient
的异步调用,我一直在四处走动,试图让 PostAsync
工作,但它通常会出现死锁,或者至少 post 响应超时,即使超时值很大,并且在 fiddler 中有一个可见的响应。
所以,过了一会儿,我决定在 HttpClient 上尝试其他几种方法,它们都有效,请先尝试。不知道为什么。
我的 WPF 按钮一直很干净 awaits
、asyncs
和 .ConfigureAwait(false)
(我认为):
按钮:
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 变体 returns 一个 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;
}
我的端点可以正常响应我们的其他软件,所以我很确定端点是可靠的,我不明白为什么 post 响应被阻止,而发送却没有。
SendAsync
可以发出任何 http 动词请求,具体取决于您如何设置 属性。 PostAsync
和类似的只是方便的方法。这些便捷方法在内部使用 SendAsync
,这就是为什么当您派生处理程序时,您只需要覆盖 SendAsync
而不是所有发送方法。
关于你的另一个问题:
当您使用 SendAsync
时,您需要创建内容并传递它。您只发送一条空消息。 500 可能意味着 api 从模型绑定中得到 null
并将你踢回去。正如@John 评论的那样。
在一个 WPF 前端的项目上工作,并试图处理对 HttpClient
的异步调用,我一直在四处走动,试图让 PostAsync
工作,但它通常会出现死锁,或者至少 post 响应超时,即使超时值很大,并且在 fiddler 中有一个可见的响应。
所以,过了一会儿,我决定在 HttpClient 上尝试其他几种方法,它们都有效,请先尝试。不知道为什么。
我的 WPF 按钮一直很干净 awaits
、asyncs
和 .ConfigureAwait(false)
(我认为):
按钮:
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 变体 returns 一个 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;
}
我的端点可以正常响应我们的其他软件,所以我很确定端点是可靠的,我不明白为什么 post 响应被阻止,而发送却没有。
SendAsync
可以发出任何 http 动词请求,具体取决于您如何设置 属性。 PostAsync
和类似的只是方便的方法。这些便捷方法在内部使用 SendAsync
,这就是为什么当您派生处理程序时,您只需要覆盖 SendAsync
而不是所有发送方法。
关于你的另一个问题:
当您使用 SendAsync
时,您需要创建内容并传递它。您只发送一条空消息。 500 可能意味着 api 从模型绑定中得到 null
并将你踢回去。正如@John 评论的那样。