我只是想调整我的方法,以便在浏览器接受 gzip 时传输压缩数据。 else
部分已经可以工作了。我只想调整 if
部分。继承人的代码:
private void writeBytes()
{
var response = this.context.Response;
if (canGzip)
{
response.AppendHeader("Content-Encoding", "gzip");
//COMPRESS WITH GZipStream
}
else
{
response.AppendHeader("Content-Length", this.responseBytes.Length.ToString());
response.ContentType = this.isScript ? "text/javascript" : "text/css";
response.AppendHeader("Content-Encoding", "utf-8");
response.ContentEncoding = Encoding.Unicode;
response.OutputStream.Write(this.responseBytes, 0, this.responseBytes.Length);
response.Flush();
}
}
最佳答案
看起来你想添加 Response.Filter,见下文。
private void writeBytes()
{
var response = this.context.Response;
bool canGzip = true;
if (canGzip)
{
Response.Filter = new System.IO.Compression.GZipStream(Response.Filter, System.IO.Compression.CompressionMode.Compress);
Response.AppendHeader("Content-Encoding", "gzip");
}
else
{
response.AppendHeader("Content-Encoding", "utf-8");
}
response.AppendHeader("Content-Length", this.responseBytes.Length.ToString());
response.ContentType = this.isScript ? "text/javascript" : "text/css";
response.ContentEncoding = Encoding.Unicode;
response.OutputStream.Write(this.responseBytes, 0, this.responseBytes.Length);
response.Flush();
}
}
关于c# - Gzip压缩asp.net c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10639337/