我已经成功地使用 Gzip 压缩设置了 II7。
我可以通过网络嗅探器检查我的 asmx 网络服务编码是 Gzip 但我如何启用 我的 C# 客户端上的 gzip 压缩,我在我的应用程序中使用 Web 服务是服务引用。
实际上我正在尝试发送大量数据,10k 数组对象,因此压缩对 bw 有很大影响。
但如何在我的 C# 客户端上启用压缩。
我试图看到很多人都遇到了同样的问题,但没有明确的答案,有些人说使用第三方工具,有些人说自定义 header 等等。
没有任何合适的方法,内置使用压缩的网络服务
最佳答案
正如@Igby Largeman 所指出的,您可以使用IIS7
在服务器上启用压缩,但这还不够。
主要思想是在客户端和服务器端设置 header :
客户:
Accept-Encoding = "gzip, deflate";
你可以通过代码实现:
var request = HttpWebRequest.Create("http://foofoo");
request.Headers["Accept"] = "application/json";
request.Headers["Accept-Encoding"] = "gzip, deflate";
或
var request = HttpWebRequest.Create("http://foofoo");
request.AutomaticDecompression = DecompressionMethods.GZip |
DecompressionMethods.Deflate;
如果您使用一些 WCF 客户端,而不是 HttpWebRequest
,您应该使用自定义检查器和调度器,如 this article :
So I used a message inspector implementing
IClientMessageInspector
andIDispatchMessageInspector
to automatically set theAcceptEncoding
andContentEncoding
http headers.This was working perfectly but I could not achieve to decompress the response on the server by first detecting the
ContentEncoding
header thus I used the work around to first try to decompress it and if it fails just try to process the request as normal.I also did this in the client pipeline and this also works.
服务器:
// This is the nearly same thing after all
Content-Encoding = "gzip" OR Content-Encoding = "deflate"
要在服务器端执行此操作,您应该在 IIS 中启用 httpCompression。
我认为你应该检查 original article得到这个工作
关于c# - Web 服务客户端和压缩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7765540/