gpt4 book ai didi

.net - 为什么HttpClient.PostAsync和PutAsync会处理内容?

转载 作者:行者123 更新时间:2023-12-04 14:44:33 26 4
gpt4 key购买 nike

HttpClient.PostAsync方法的行为是处理提供的HttpContent对象。

有许多方法可以解决此问题,包括为客户端上的每个调用构造一个新的HttpContent或将内容加载到流中并更改指针。

我想知道为什么调用此方法会自动调用对IDisposable参数的处理?据我所知,这不是.NET中的常见现象

还要注意的是,在幂等的PUT请求中也观察到了此行为,因此以防止再次发送信息为前提的前提似乎并不正确。

最佳答案

我无法立即在referencesource上找到实现,但是WCF源也包含它。您要查找的方法是 DisposeRequestContent(HttpRequestMessage) ,附带的注释说明了这一点:

When a request completes, HttpClient disposes the request content so the user doesn't have to. This also ensures that a HttpContent object is only sent once using HttpClient (similar to HttpRequestMessages that can also be sent only once).


HttpContent content = request.Content;
if (content != null)
{
content.Dispose();
}

基本上,确保您不会两次发送相同的响应(他们认为它们是不好的/不常见的/使用不周的用例)是一种安全措施。

关于.net - 为什么HttpClient.PostAsync和PutAsync会处理内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25495394/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com