c# - Web 服务客户端和压缩

标签 c# arrays web-services iis-7 asmx

我已经成功地使用 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 and IDispatchMessageInspector to automatically set the AcceptEncoding and ContentEncoding 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/

相关文章:

c - 为什么 "char array"需要两个 "[]"而 "int array"需要一个 "[]"?

PHP json_encode - 无效的 JSON

java - 使用soapUI 和基于HTTPS 的WSDL 生成Web 服务类

c# - 部分类构造器

c# - 升级.Net版本后无法加载文件或程序集错误

javascript - 正则表达式和数组 : Most efficient approach

sql - 通过 .NET ReportingService Web 服务调用连接时为 "Unable to Connect to the Remote Server"

c# - 如何在 Dict<int,List<Tuple<string,string>>> 中查找键,以便列表包含具有给定 Item1 和 Items 的元素

c# - Npgsql异常 : "Unknown message code: 74" on dotnet ef database update

c - 赋值从指针生成整数而不进行强制转换 [-Wint-conversion