开发者

Webservice Client and Compression

开发者 https://www.devze.com 2023-04-12 19:17 出处:网络
I have Managed to Setup II7 with Gzip Compression . I can check via web sniffer that my asmx 开发者_JAVA百科web service encoding is Gzip but how to i enable

I have Managed to Setup II7 with Gzip Compression .

I can check via web sniffer that my asmx 开发者_JAVA百科web service encoding is Gzip but how to i enable gzip Compression on my C# Client , i am using the Web Service is Service reference in my application.

Actually i am trying to send large amount of data , 10k objects of array so Compression with be great effect on bw.

but how do I enable Compression on my C# Client.

i am trying to see that many people sees same problem but there nothing clear answer some says use third party tools or some says about custom headers etc etc .

is not there any proper way , built in to consume Compressed web service


As @Igby Largeman pointed out, you can use your IIS7 to enable the compression on the server, but this is not enough.
The main idea is to set the headers on the client side and server side:

Client:

Accept-Encoding = "gzip, deflate";

You can achieve this by code:

var request = HttpWebRequest.Create("http://foofoo");
request.Headers["Accept"] = "application/json";
request.Headers["Accept-Encoding"] = "gzip, deflate";

or

var request = HttpWebRequest.Create("http://foofoo");
request.AutomaticDecompression = DecompressionMethods.GZip |  
  DecompressionMethods.Deflate;

If you use some WCF client, and not the HttpWebRequest, you should use custom inspector and dispatcher, like in 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.

Server:

// This is the nearly same thing after all
Content-Encoding = "gzip" OR Content-Encoding = "deflate"

To do this on the Server side, you should enable httpCompression in the IIS.
I think you should check the original article to get this work

0

精彩评论

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