Donate

Uncompress GZip Response Using WebClient In C#

In a scenario where you want to download xml data using webclient, the response from firefox or any browser will display the xml file correctly. However, using a web request/webclient.downloadstring to download the xml file, the response is somewhat corrupted or in hashed form.
The response headers are the following:
1. Content-Type - application/xml
2. Content-Encoding - gzip
As you can see, the content encoding is of type gzip. The solution is to override the web request method to something like this:
class DecompressGzipResponse : WebClient  
{  
      protected override WebRequest GetWebRequest(Uri address)  
      {  
        HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);  
        request.AutomaticDecompression = DecompressionMethods.GZip   
                 | DecompressionMethods.Deflate;  
        return request;  
      }  
}  
 
//to use this in the program (main function)
DecompressGzipResponse client = new DecompressGzipResponse ();  
ListingSource = client.DownloadString("http://www.boston.com/multimedia/products/realestate/listings.xml");

Reference: GZIP and HTTP Response

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.

Invalid nested tag div found, expected closing tag input