简体   繁体   English

在Silverlight中请求GZipped内容

[英]Request GZipped content in Silverlight

I'm having trouble to request Gzipped content with Silverlight 5. To reduce used bandwidth I want to use gzip for the content that's requested from our webservice, but unfortunately this does not work. 我在使用Silverlight 5请求Gzip压缩的内容时遇到麻烦。为了减少使用的带宽,我想对我们的Web服务所请求的内容使用gzip,但不幸的是,这无法正常工作。

I'm using the Client Stack and send a HttpWebRequest. 我正在使用客户端堆栈并发送HttpWebRequest。 I tried to set the Accept-Encoding header, but it tells me that I'm not allowed to change this directly - and there's no other property for it! 我试图设置Accept-Encoding标头,但它告诉我不允许直接更改它-并且没有其他属性!

So how can I send a custom Accept-Encoding header? 那么如何发送自定义的Accept-Encoding标头?

This is the code I have in my App-Constructor: 这是我在应用程序构造函数中的代码:

WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);

This is how I create the request and set the header: 这是我创建请求并设置标头的方式:

var webRequest = (HttpWebRequest)WebRequest.Create(requestUri);
webRequest.Headers["Accept-Encoding"] = "gzip, deflate";

This is the exception: 这是例外:

{System.ArgumentException: The 'Accept-Encoding' header cannot be modified directly.
Parameter name: name
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at MyClass}

不幸的答案是:如果要gzip,请不要使用客户端堆栈(浏览器堆栈已经为您处理了)

In this case I'm fortunate enough to control the service side as well as the client side. 在这种情况下,我很幸运地可以控制服务端和客户端。 What I have done now is adding an additional custom header that will essentially override the Accept-Encoding header. 我现在所做的是添加一个附加的自定义标头,该标头将实质上覆盖Accept-Encoding标头。 I call it "Accept-Encoding-Override". 我称其为“ Accept-Encoding-Override”。 When my service encounters this header it'll compress the the content no matter what's in the Accept-Encoding header. 当我的服务遇到此标头时,无论Accept-Encoding标头中的内容如何,​​它都会压缩内容。 On the client silverlight side I check the Content-Encoding header in the response and if necessary unzip it using SharpZipLib (DotNetZip did not work). 在客户端Silverlight端,我检查响应中的Content-Encoding标头,并在必要时使用SharpZipLib将其解压缩(DotNetZip无效)。

I still mark dotMorten as his response is the actually answer to my question. 我仍然将dotMorten标记为他的回答实际上是对我的问题的回答。 There's no way to set the header when using ClientStack. 使用ClientStack时无法设置标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM