gpt4 book ai didi

c# - 将 gzip 发布到 influxdb 结果为 "unexpected EOF"

转载 作者:行者123 更新时间:2023-12-02 16:09:42 24 4
gpt4 key购买 nike

尝试向 influxdb 写入 gzip post 请求,但收到“意外的 EOF”

以下代码是在 F# 中编写的,但很简单。您可以在脑海中轻松地将其翻译成 C#

let gzip(s: string) =
let bytes = Encoding.UTF8.GetBytes s
use ms = new MemoryStream()
use gz = new GZipStream(ms, CompressionLevel.Fastest)
(gz.Write(bytes, 0, bytes.Length))
ms.ToArray()
let toGzipContent(s: string) =
let content = new ByteArrayContent(gzip s)
content.Headers.ContentEncoding.Add "gzip"
content.Headers.ContentType <- MediaTypeHeaderValue("text/plain", CharSet = "utf-8")
content
let postTest() =
let client = new HttpClient()

let hash = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{cfg.user}:{cfg.password}"))
client.DefaultRequestHeaders.Authorization <- AuthenticationHeaderValue("Basic", hash )
client.DefaultRequestHeaders.ConnectionClose <- false

let content =
"d1fcd8ad-1dea-4a91-b1ed-eafffd497ade,tag=tag0 field=0.5177723544266878 1626025134320"
|> toGzipContent
let resp = client.PostAsync(cfg.url, content)
resp

influxdb 响应:

  StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Request-Id: a1edc900-e26e-11eb-b78b-244bfe8c4492
X-Influxdb-Build: OSS
X-Influxdb-Error: unexpected EOF
X-Influxdb-Version: 1.8.6
X-Request-ID: a1edc900-e26e-11eb-b78b-244bfe8c4492
Date: Sun, 11 Jul 2021 17:37:15 GMT
Content-Type: application/json
Content-Length: 27
}

发送纯文本有效,但 gzipped 无效。这里有什么问题?已经研究了 influxdb .NET 客户端实现,没有发现任何问题

最佳答案

最后,我只是放错了大括号:

let gzip(s: string) =
let bytes = Encoding.UTF8.GetBytes s
use ms = new MemoryStream()
( use gz = new GZipStream(ms, CompressionLevel.Fastest)
gz.Write(bytes, 0, bytes.Length)
)
ms.ToArray()

更详细地说,问题在于处理嵌套 using 的方式和时间。在 F# 中,在 C# 中,从允许您编写以下内容的最新版本开始:

public byte[] gzip(s: string)
{
var bytes = Encoding.UTF8.GetBytes(s);
using var ms = new MemoryStream();
using (var gz = new GZipStream(ms, CompressionLevel.Fastest))
{
gz.Write(bytes, 0, bytes.Length);
// the most nested block, writing to the array, should explicitly end here
// to let gz be disposed and flushed
}
return ms.ToArray();
}

就像@CaringDev 回答的那样,gz 流在创建输出数组时尚未刷新。

关于c# - 将 gzip 发布到 influxdb 结果为 "unexpected EOF",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68338777/

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