POST gzip 到 influxdb 结果 "unexpected EOF"
POST gzip to influxdb results in "unexpected EOF"
尝试向 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 客户端实现,没有发现任何问题
gzipped content
为空,需要刷新流。在写入和转换底层 ms.ToArray()
之间插入 gz.Flush()
应该可以解决这个问题。
最后,我只是错放了大括号:
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()
更详细地说,问题在于如何以及何时处理嵌套使用。在 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
流在创建输出数组时尚未刷新。
尝试向 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 客户端实现,没有发现任何问题
gzipped content
为空,需要刷新流。在写入和转换底层 ms.ToArray()
之间插入 gz.Flush()
应该可以解决这个问题。
最后,我只是错放了大括号:
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()
更详细地说,问题在于如何以及何时处理嵌套使用。在 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
流在创建输出数组时尚未刷新。