The Net.WebClient class doesn't support HTTP compression, eg, when you add the Accept-Encoding: gzip,deflate header to your request:
Dim wc As New Net.WebClient
'-- google will not gzip the content if the User-Agent header is missing!
wc.Headers.Add("User-Agent", strHttpUserAgent)
wc.Headers.Add("Accept-Encoding", "gzip,deflate")
'-- download the target URL into a byte array
Dim b() As Byte = wc.DownloadData(strUrl)
What you get is a gzipped array of bytes. It's pretty easy to add the missing gzip support, though. First, download the SharpZipLib and add a reference to ICSharpCode.SharpZipLib to your project. Then it's only a few more lines of code..
Dim gz As New GZip.GZipInputStream(New MemoryStream(b))
Dim intSizeRead As Integer
Dim unzipBytes(intChunkSize) As Byte
Dim OutputStream As New MemoryStream
While True
'-- this decompresses a chunk
'-- remember the output will be larger than the input (one would hope)
intSizeRead = gz.Read(unzipBytes, 0, intChunkSize)
If intSizeRead > 0 Then
OutputStream.Write(unzipBytes, 0, intSizeRead)
Else
Exit While
End If
End While
'-- convert our decompressed bytestream into a UTF-8 string
Return System.Text.Encoding.UTF8.GetString(OutputStream.ToArray)
And voila, the bandwidth, you have saved eet! How do I know this actually works? Using my network sniffer of course..
Posted by Jeff Atwood View blog reactions
« Sniff this! Java vs. .NET RegEx performance »
Of course in the REAL world, before un-gzipping, you'd actually check to make sure the response header contained the
Content-Encoding: gzip
header indicating the byte array you got was actually compressed.. right? right?
Jeff Atwood on August 27, 2004 11:45 PMHow about web services? Could they also be encoded with gzip?
Dag Konig on August 28, 2004 02:04 AMDefinitely! For example..
http://www.codeproject.com/cs/webservices/WebServiceZipFilter.asp
Jeff Atwood on August 28, 2004 07:58 PMalso...
Retrieving Data from Web Services using Standard HTTP 1.1 Compression
http://www.dotnetjunkies.com/Tutorial/90D3B3E0-6544-4594-B3BA-E41D8F381324.dcik
Jeff Atwood on August 31, 2004 02:10 PMBEWARE! The SharpLipZip does not seem to properly support "deflate" type compression. At least I can't find any references in Google to anyone who actually GOT it to work.. fortunately this type of compression is rare. But still, I'd modify...
wc.Headers.Add("Accept-Encoding", "gzip,deflate")
to be just
wc.Headers.Add("Accept-Encoding", "gzip")
and prevent any future problems.
Jeff Atwood on January 3, 2005 03:45 AMSee my updated blog entry on this:
http://www.codinghorror.com/blog/archives/000182.html
Jeff Atwood on January 12, 2005 01:01 AMFor webservices, assuming compression is enabled on the server side, all you have to do is set the EnableDecompression property to true on your proxy class generated with the wsdl tool.
In fact the .NET 2.0 docs clearly states EnableDecompression is set to true by default so all you need is to make the server send it gzip compressed.
Look it up in the docs or google on "EnableDecompression".
PL on April 9, 2007 07:45 AMWe are using ICSharpCode in Sitemap Writer Pro for compression of big sitemap files.
http://www.sitemapwriter.com
| Content (c) 2008 Jeff Atwood. Logo image used with permission of the author. (c) 1993 Steven C. McConnell. All Rights Reserved. |