From 85db7223849d1e265606b404e0a29d8f67fe8ee9 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Thu, 22 Oct 2026 03:28:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9AA=E2=86=92B=20?= =?UTF-8?q?=E7=9A=84=20POST=20=E5=85=A8=E9=83=A8=E6=97=A0=E6=95=88?= =?UTF-8?q?=EF=BC=88JsonContent=20=E4=B8=8D=E5=B8=A6=20Content-Length?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 边端是手写的 HTTP 读取,只认 Content-Length;而 A 侧一直用 JsonContent.Create(),它产生的是 chunked 请求 —— 边端把 body 读成空, /check 直接 400,界面就显示「设备管理服务无响应,改由本机校验」。 也就是说局域网共用取流池这条路,用真实客户端从来没成功过, 此前只有 curl 验证过所以一直没暴露。 - EdgeClient 改用 StringContent(显式 Content-Length) - 边端遇到 chunked 请求时明确记一行,不再让调用方看到「空 body」的怪错误 实测:JsonContent → 400 contentLength=null;StringContent → 200 contentLength=186 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- desktop/Services/Agent/EdgeHttpService.cs | 9 +++++++++ desktop/Services/Edge/EdgeClient.cs | 9 ++++++++- desktop/YuncangTrace.LocalService.csproj | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/desktop/Services/Agent/EdgeHttpService.cs b/desktop/Services/Agent/EdgeHttpService.cs index d8da204..f68d554 100644 --- a/desktop/Services/Agent/EdgeHttpService.cs +++ b/desktop/Services/Agent/EdgeHttpService.cs @@ -602,6 +602,15 @@ public sealed class EdgeHttpService ? parsed : 0; + if (contentLength == 0 + && headers.TryGetValue("Transfer-Encoding", out var transferEncoding) + && transferEncoding.Contains("chunked", StringComparison.OrdinalIgnoreCase)) + { + // 手写解析只认 Content-Length:真遇到 chunked 就明说,别让调用方看到「空 body」的怪错误 + AppLog.Write($"[edge] 收到 chunked 请求 {method} {path}(不支持,请用 Content-Length)"); + return null; + } + if (contentLength is < 0 or > MaxBodyBytes) return null; var bodyStart = headerEnd + 4; diff --git a/desktop/Services/Edge/EdgeClient.cs b/desktop/Services/Edge/EdgeClient.cs index 9f555d8..c65f669 100644 --- a/desktop/Services/Edge/EdgeClient.cs +++ b/desktop/Services/Edge/EdgeClient.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Net.Http.Json; +using System.Text; using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -215,7 +216,13 @@ public sealed class EdgeClient : IDisposable if (payload is not null) { - request.Content = JsonContent.Create(payload, options: HikWorkerJson.Options); + // ⚠️ 不能用 JsonContent.Create:它不带 Content-Length(走 chunked), + // 而边端是手写的 HTTP 读取、只认 Content-Length,会把 body 读成空 → 400。 + // 先用 StringContent 序列化成带长度的请求。 + request.Content = new StringContent( + JsonSerializer.Serialize(payload, HikWorkerJson.Options), + Encoding.UTF8, + "application/json"); } try diff --git a/desktop/YuncangTrace.LocalService.csproj b/desktop/YuncangTrace.LocalService.csproj index dd2a7d9..74c7f5f 100644 --- a/desktop/YuncangTrace.LocalService.csproj +++ b/desktop/YuncangTrace.LocalService.csproj @@ -42,7 +42,7 @@ http://127.0.0.1:7300 https://packs.example.com - 1.0.109 + 1.0.110 浜戜粨瑙嗛婧簮杞欢 浜戜粨 浜戜粨瑙嗛婧簮杞欢锛堟湰鏈烘湇鍔★級