You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 02:41:57