Imports System.Net
Imports System.IO
Imports System.IO.Compression
Imports System.Text
Function GZip2Html(ByVal url AsString) AsString
‘ HTTP请求
Dim req As HttpWebRequest = HttpWebRequest.Create(url)
req.Method ="GET"‘GET 或 POST
req.Accept ="*/*"
req.UserAgent ="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1;)"‘这里模拟 XP系统 IE7 的请求字段
req.Referer ="来源地址"
‘ 一些头信息的模拟
req.Headers("Accept-Language") ="zh-cn"
req.Headers("UA-CPU") ="x86"
req.Headers("Accept-Encoding") ="gzip, deflate"
‘ HTTP获取
Dim res As HttpWebResponse = req.GetResponse
‘ 获取数据文本编码
‘Dim enc As Encoding = Encoding.GetEncoding(res.CharacterSet) ‘如果出现乱码请换下面这种方式
Dim enc As Encoding = Encoding.Default
‘ 创建一个GZip解压流
Dim gz AsNew GZipStream(res.GetResponseStream, CompressionMode.Decompress)
‘ 用一个临时内存流来保存解压数据
Dim ms AsNew MemoryStream
‘ 缓冲数据
Dim buf(99) AsByte, i AsInteger=0
‘ 不断从流中解压数据
WhileTrue
i = gz.Read(buf, 0, 100)
If i =0ThenExitWhile
ms.Write(buf, 0, i)
EndWhile
‘ 将数据转换为字符
Dim ret AsString= enc.GetString(ms.ToArray)
‘ 关闭所有的流
gz.Close()
ms.Close()
res.Close()
Return ret
End Function