求助各路大神!获取网页源码中GZIP网页乱码问题,百度谷歌了一整月了。。痛苦
求拜各路大神能给点帮助,实在没办法了,都折腾了快两个月了都解决不了!!
下面是在网上找的获取网页源码的方法:
Option Explicit
Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Public Const INTERNET_OPEN_TYPE_DIRECT = 1
Public Const INTERNET_OPEN_TYPE_PROXY = 3
Public Const scUserAgent = "Microsoft Internet Explorer 6.0"
Public Const INTERNET_FLAG_RELOAD = &H80000000
Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Public Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hOpen As Long, ByVal sUrl As String, ByVal sHeaders As String, ByVal lLength As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Public Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
Public Function GetUrlStr(Url As String) As String '调用时 URL 参数一定要带 http:// 前缀.
Dim hOpen As Long
Dim hOpenUrl As Long
Dim sUrl As String
Dim bDoLoop As Boolean
Dim bRet As Boolean
Dim sReadBuffer As String * 2048
Dim lNumberOfBytesRead As Long
Dim sBuffer As String
sUrl = Url
DoEvents
hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
hOpenUrl = InternetOpenUrl(hOpen, sUrl, vbNullString, 0, INTERNET_FLAG_RELOAD, 0)
bDoLoop = True
While bDoLoop
DoEvents
sReadBuffer = vbNullString
bRet = InternetReadFile(hOpenUrl, sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead)
sBuffer = sBuffer & Left$(sReadBuffer, lNumberOfBytesRead)
If Not CBool(lNumberOfBytesRead) Then bDoLoop = False
Wend
GetUrlStr = sBuffer
If hOpenUrl <> 0 Then InternetCloseHandle (hOpenUrl)
If hOpen <> 0 Then InternetCloseHandle (hOpen)
End Function
这个方法遇到UTF8编码或者GZIP压缩的网页就会乱码。UTF8的问题我已解决了,转换一下就行,但是GZIP的实在不知道怎么弄,网上也有些所谓的GZIP解压的东西,但是耐何水平有限,看不懂,也不知道怎么用,这上面的代码获取内容后都存在sBuffer这个变量里面,不知道怎么进一步处理了,还有也不知道怎么事先就判断网页是否为GZIP压缩的。。。试了好久都没什么结果。。快疯了。。小弟只是个新手,也没什么分,能给的都给了,希望各位大神打救一下。。
拜托各位好心的不要把网上那一大堆贴来贴去的东西再贴过来,都看到疯了。。。还有,不要跟我说什么用其他方法用控件什么的,我原先是用webbrowser控件实现的,但是实在太慢了,我要获取的网页是非常多的,要的是速度而且内容要全,有些方法获取的内容都不全的。
最好能根据上面的代码给出GZIP判断及解压的代码,网上那一堆真看不明。。。如果实在不行有其他办法能满足上面的要求的也可以,就是速度要快,内容要全,不会卡(比如URLDownloadToFile网络差就卡到界面像死掉)。。
[解决办法]
帮你顶顶吧,高人们一会就来,我打打前锋。
[解决办法]