.NET HttpClient获取大体积JSON数据时遇提前终止异常求助
大体积JSON数据获取失败问题排查与求助
核心问题:无法正常获取大体积数据。调用API获取10000行(800MB)的JSON列表时,报错:Error while copying content to stream... (inner exception the response ended prematurely),但获取100行(10MB)数据可正常运行——因数据库老旧且存储Blob数据,导致单请求数据体积庞大。
已尝试操作
在请求中加入HttpCompletionOption.ResponseHeadersRead参数:
response = await HTTPHelper.Client.SendAsync(req, HttpCompletionOption.ResponseHeadersRead);
此时请求可执行,但错误转移至下一行代码:
var responseData = await response.Content.ReadFromJsonAsync<List<InspForm>>();
异常调用栈
This exception was originally thrown at this call stack: System.Net.Http.HttpConnection.FillAsync(bool) System.Net.Http.HttpConnection.ChunkedEncodingReadStream.ReadAsyncCore(System.Memory<byte>, System.Threading.CancellationToken) System.Text.Json.Serialization.ReadBufferState.ReadFromStreamAsync(System.IO.Stream, System.Threading.CancellationToken, bool) System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable<TResult>.ConfiguredValueTaskAwaiter.GetResult() System.Text.Json.JsonSerializer.ReadFromStreamAsync<TValue>(System.IO.Stream, System.Text.Json.Serialization.Metadata.JsonTypeInfo, System.Threading.CancellationToken) System.Threading.Tasks.ValueTask<TResult>.Result.get() System.Net.Http.Json.HttpContentJsonExtensions.ReadFromJsonAsyncCore<T>(System.Net.Http.HttpContent, System.Text.Encoding, System.Text.Json.JsonSerializerOptions, System.Threading.CancellationToken) SyncClient.Form1.btnSync_Click(object, System.EventArgs) in Form1.cs
响应头信息
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Date: Fri, 18 Nov 2022 13:47:03 GMT
Server: Kestrel
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
}
恳请提供可行的解决方案,感谢帮助!
内容的提问来源于stack exchange,提问作者DemiGod
相关产品推荐
相关产品推荐

