Net.WebClient和GZip
作者:admin 时间:2015-2-11 7:46:44 浏览:Net.WebClient 类不支持HTTP压缩,例如,当你添加Accept-Encoding: gzip,deflate
到请求头部时:
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)
你得到的是经过gzip压缩过的字节数组。
很容易让WebClient 支持GZip,不过,首先要下载SharpZipLib (http://icsharpcode.github.io/SharpZipLib/),然后添加引用 ICSharpCode.SharpZipLib 到你的工程。然后,只是多了几行代码。。。
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)
要使asp.net请求支持GZip,可以使用HttpWebRequest,HttpWebRequest 支持gzip/deflate 压缩。请参考前一篇文章《【解决】asp.net请求header添加gzip后抓网页源码乱码问题》。
- 站长推荐