using System;using System.IO;using System.Threading;using Nancy;using Nancy.Hosting.Self;namespace TestNancy{ public class ChunkedResponse : Response { public ChunkedResponse() { ContentType = "text/HTML; charset=utf-8"; Contents = stream => { using (var streamWriter = new StreamWriter(stream)) { while (true) { streamWriter.Writeline("Hello"); streamWriter.Flush(); Thread.Sleep(1000); } } }; } } public class HomeModule : NancyModule { public HomeModule() { Get["/"] = args => new ChunkedResponse(); } } public class Program { public static voID Main() { using (var host = new NancyHost(new Uri("http://localhost:1234"))) { host.Start(); Console.Readline(); } } }}
现在我想为流添加压缩以压缩带宽量.出于某种原因,在浏览器中进行测试时,我无法看到任何结果.我已经尝试了很多组合来达到预期的效果,但这就是我现在所拥有的:
using System; using System.IO; using System.IO.Compression; using System.Threading; using Nancy; using Nancy.Hosting.Self;namespace TestNancy { public class ChunkedResponse : Response { public ChunkedResponse() { headers["content-encoding"] = "gzip"; ContentType = "text/HTML; charset=utf-8"; Contents = stream => { using (var gzip = new GZipStream(stream,CompressionMode.Compress)) using (var streamWriter = new StreamWriter(gzip)) { while (true) { streamWriter.Writeline("Hello"); streamWriter.Flush(); Thread.Sleep(1000); } } }; } } public class HomeModule : NancyModule { public HomeModule() { Get["/"] = args => new ChunkedResponse(); } } public class Program { public static voID Main() { using (var host = new NancyHost(new Uri("http://localhost:1234"))) { host.Start(); Console.Readline(); } } } }
我正在寻找帮助,要么告诉我我在http协议上做错了什么(例如我尝试添加http1.1中描述的块长度,这不起作用),或者帮助关于Nancy,它做了我没做过的事情对于.
解决方法 问题似乎是在Gzip框架的实现中,因为它在关闭之前从未写入输出流,我只是使用SharpZiplib,你的代码似乎对我有用,这是我的修改
public class ChunkedResponse : Response{ public ChunkedResponse() { headers["transfer-encoding"] = "chunked"; headers["content-encoding"] = "gzip"; ContentType = "text/HTML; charset=utf-8"; Contents = stream => { var gzip = new ICSharpCode.SharpZiplib.GZip.GZipOutputStream(stream); using (var streamWriter = new StreamWriter(gzip)) { while (true) { streamWriter.Writeline("Hello"); gzip.Flush(); streamWriter.Flush(); Thread.Sleep(1000); } } }; }}public class HomeModule : NancyModule{ public HomeModule() { Get["/"] = args => new ChunkedResponse(); }}public class Program{ public static voID Main() { using (var host = new NancyHost(new HostConfiguration{AllowChunkedEnCoding = true},new Uri("http://localhost:1234"))) { host.Start(); Console.Readline(); } }}
SharpZiplib的Nuget包:PM>安装包SharpZiplib
总结以上是内存溢出为你收集整理的c# – 使用NancyFX自托管的分块压缩响应全部内容,希望文章能够帮你解决c# – 使用NancyFX自托管的分块压缩响应所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)