HTTP 応答に gzip または deflate 圧縮を適用すると、JSON 構造の最後のブラケットが失われているようです。例えば:
圧縮なしの結果:
{"alist":{"P_1":0,"P_2":0,"P_3":0}}
ブラウザが受信した圧縮結果:
{"alist":{"P_1":0,"P_2":0,"P_3":0}
圧縮せずに応答を書くとき、私は次のことをしています:
byte[] buffer = Encoding.UTF8.GetBytes(responseContent);
context.Response.ContentLength64 = buffer.Length;
context.Response.ContentType = ContentTypeJson;
Stream outputStream = context.Response.OutputStream;
outputStream.Write(buffer, 0, buffer.Length);
outputStream.Close();
あるいは、呼び出し元が Accept-Encoding 要求ヘッダーを提供する場合、次のように圧縮して応答を書き込もうとします。
byte[] buffer = Encoding.UTF8.GetBytes(responseContent);
byte[] compressedBuffer;
using (var memoryStream = new MemoryStream())
{
using (Stream compressionStream = new DeflateStream(memoryStream, CompressionMode.Compress, false))
{
compressionStream.Write(buffer, 0, buffer.Length);
compressedBuffer = memoryStream.ToArray();
compressionStream.Close();
}
memoryStream.Close();
}
context.Response.ContentLength64 = compressedBuffer.Length;
context.Response.ContentType = ContentTypeJson;
Stream outputStream = context.Response.OutputStream;
outputStream.Write(compressedBuffer, 0, compressedBuffer.Length);
outputStream.Close();
それが役立つ場合、私は System.Net.HttpListener を使用しているため、自分でこれを行う必要があります。この切り捨てが発生している理由を誰か知っていますか?