开发者

C# HttpListener Response + GZipStream

开发者 https://www.devze.com 2023-04-05 00:45 出处:网络
I use HttpListener for my own http server (I do not use IIS). I want to compress my OutputStream by GZip compression:

I use HttpListener for my own http server (I do not use IIS). I want to compress my OutputStream by GZip compression:

byte[] refBuffer = Encoding.UTF8.GetBytes(...some data sourc开发者_运维问答e...);

var varByteStream = new MemoryStream(refBuffer);

System.IO.Compression.GZipStream refGZipStream = new GZipStream(varByteStream, CompressionMode.Compress, false);

refGZipStream.BaseStream.CopyTo(refHttpListenerContext.Response.OutputStream);

refHttpListenerContext.Response.AddHeader("Content-Encoding", "gzip");

But I getting error in Chrome:

ERR_CONTENT_DECODING_FAILED

If I remove AddHeader, then it works, but the size of response is not seems being compressed. What am I doing wrong?


The problem is that your transfer is going in the wrong direction. What you want to do is attach the GZipStream to the Response.OutputStream and then call CopyTo on the MemoryStream, passing in the GZipStream, like so:

refHttpListenerContext.Response.AddHeader("Content-Encoding", "gzip"); 

byte[] refBuffer = Encoding.UTF8.GetBytes(...some data source...); 

var varByteStream = new MemoryStream(refBuffer); 

System.IO.Compression.GZipStream refGZipStream = new GZipStream(refHttpListenerContext.Response.OutputStream, CompressionMode.Compress, false); 

varByteStream.CopyTo(refGZipStream); 
refGZipStream.Flush();


The first problem (as mentioned by Brent M Spell) is the wrong position of the header. The second is that you don't use properly the GZipStream. This stream requires a "top" stream to write to, meaning an empty stream (you fill it with your buffer). Having an empty "top" stream then all you have to do is to write on GZipStream your buffer. As a result the memory stream will be filled by the compressed content. So you need something like:

byte[] buffer = ....;

using (var ms = new MemoryStream())
{
    using (var zip = new GZipStream(ms, CompressionMode.Compress, true))
    zip.Write(buffer, 0, buffer.Length);
    buffer = ms.ToArray();
}

response.AddHeader("Content-Encoding", "gzip");
response.ContentLength64 = buffer.Length;

response.OutputStream.Write(buffer, 0, buffer.Length);


Hopeful this might help, they discuss how to get GZIP working.

Sockets in C#: How to get the response stream?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号