Question

I have this piece of code which works fine in WindowsForm application, but I need to re use the exact (or something similar) code in Windows Phone 7.1 application.

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(textBoxUrl.Text);
req.UserAgent = "MOZILLA/5.0 (WINDOWS NT 6.1; WOW64) APPLEWEBKIT/537.1 (KHTML, LIKE GECKO) CHROME/21.0.1180.75 SAFARI/537.1";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.Headers.Add("Accept-Encoding", "gzip,deflate");

GZipStream zip = new GZipStream(req.GetResponse().GetResponseStream(),
                                                      CompressionMode.Decompress);
var reader = new StreamReader(zip);
var page = reader.ReadToEnd();

When I move this code to Windows Phone, There is no GZipStream. Also the HttpWebRequest.Headers does not have an Add method in Windows Phone environment. Would someone kindly tell me a workaround to this problem?

Was it helpful?

Solution

You really should be using Microsoft.Net.Http from NuGet, which is a Portable Class Library from Microsoft that brings the HttpClient methods from .NET 4.5 to all current versions of the Framework, and includes GZIP and DEFLATE support.

It would also mean that all of your code for the function you are attempting to complete would stay the same, no matter what platform you are targeting.

I should also point out that the code you are using will work when the stream is actually GZIP encoded, but will not work if the stream is not compressed. Which is all the more reason you should be using HttpClient.

HTH

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top