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.
I’m using the Client Stack and send a 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!
So how can I send a custom Accept-Encoding header?
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}
The unfortunate answer is: don’t use the client stack if you want to gzip (the browser stack handles this for you already)