gpt4 book ai didi

.net - System.Net.Http.HttpClient 是否受到 HttpWebRequest.AllowWriteStreamBuffering 的影响?

转载 作者:行者123 更新时间:2023-12-04 02:09:19 35 4
gpt4 key购买 nike

我一直在尝试使用 System.Net.Http.HttpClient发布一个更大的文件(+1GB),但它抛出一个 SystemOutOfMemory异常(exception):

at System.Net.ScatterGatherBuffers.AllocateMemoryChunk(Int32 newSize)
at System.Net.ScatterGatherBuffers..ctor(Int64 totalSize)
at System.Net.ConnectStream.EnableWriteBuffering()
at System.Net.HttpWebRequest.SetRequestSubmitDone(ConnectStream submitStream)
at System.Net.Connection.CompleteStartRequest(Boolean onSubmitThread, HttpWebRequest request, TriState needReConnect)
at System.Net.Connection.SubmitRequest(HttpWebRequest request, Boolean forcedsubmit)
at System.Net.ServicePoint.SubmitRequest(HttpWebRequest request, String connName)
at System.Net.HttpWebRequest.SubmitRequest(ServicePoint servicePoint)
at System.Net.HttpWebRequest.BeginGetRequestStream(AsyncCallback callback, Object state)
at System.Net.Http.HttpClientHandler.StartGettingRequestStream(RequestState state)
at System.Net.Http.HttpClientHandler.PrepareAndStartContentUpload(RequestState state)

显然, HttpWebRequest 也出现了类似的问题。如此处所讨论: http://support.microsoft.com/kb/908573 .

有没有办法设置 AllowWriteStreamBufferingfalse 的底层 Web 请求?我找不到任何。

干杯,

最佳答案

为了节省其他感兴趣的人的时间,我正在回答我自己的问题。

经过几次测试,异常似乎归结为与 HttpWebRequest 相同的问题。正如问题中所讨论的那样。我使用 Microsoft.AspNet.WebApi 版本 4.0.20710.0。

下面是两段等效的代码;前者在大文件上失败,而后者工作正常。

顺便说一句,尽管 HttpClient 的整体利益问题变得非常明显:-)

使用 HttpClient

var clientRef = new System.Net.Http.HttpClient(
new HttpClientHandler()
{
Credentials = new System.Net.NetworkCredential(MyUsername, MyPassword)
});
clientRef.BaseAddress = new Uri(serverAddress);
clientRef.DefaultRequestHeaders.ExpectContinue = false;
clientRef.PostAsync(
MyFavoriteURL,
new System.Net.Http.StreamContent(inputStream)).ContinueWith(
requestTask =>
{
HttpResponseMessage response = requestTask.Result;
response.EnsureSuccessStatusCode();
}, TaskContinuationOptions.LongRunning).Wait();

使用 HttpWebRequest
// Preauthenticate
var req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(MyFavoriteURL);
req.Credentials = new System.Net.NetworkCredential(MyUsername, MyPassword);
req.Method = "POST";
req.PreAuthenticate = true;
req.Timeout = 10000;
using (var resp = (System.Net.HttpWebResponse)req.GetResponse())
{
if (resp.StatusCode != System.Net.HttpStatusCode.Accepted && resp.StatusCode != System.Net.HttpStatusCode.OK)
{
throw new Exception("Authentication error");
}
}

// Upload
req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(MyFavoriteURL);
req.Credentials = new System.Net.NetworkCredential(MyUsername, MyPassword);
req.Method = "POST";
req.PreAuthenticate = true;
req.Timeout = 1200000;
req.ContentLength = inputStream.Length;
req.ContentType = "application/binary";
req.AllowWriteStreamBuffering = false;
req.Headers.ExpectContinue = false;
using (var reqStream = req.GetRequestStream())
{
inputStream.CopyTo(reqStream);
}

using (var resp = (System.Net.HttpWebResponse)req.GetResponse())
{
if (resp.StatusCode != System.Net.HttpStatusCode.Accepted && resp.StatusCode != System.Net.HttpStatusCode.OK)
{
throw new Exception("Error uploading document");
}
}

关于.net - System.Net.Http.HttpClient 是否受到 HttpWebRequest.AllowWriteStreamBuffering 的影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16037249/

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