這篇文章主要介紹了VBS獲取GZIP壓縮的HTTP內(nèi)容的實(shí)現(xiàn)代碼,需要的朋友可以參考下
不少網(wǎng)站為了提高加載速度,啟用HTTP服務(wù)器的GZIP壓縮功能,當(dāng)客戶(hù)端發(fā)送的HTTP請(qǐng)求中聲明可以接受GZIP編碼時(shí),服務(wù)器自動(dòng)對(duì)HTTP響應(yīng)內(nèi)容進(jìn)行GZIP壓縮。但是,在VBS中想自動(dòng)對(duì)GZIP編碼進(jìn)行解壓就沒(méi)有那么容易了。
不同組件對(duì)GZIP壓縮的處理不盡相同,首先看Msxml2.XMLHTTP:
'By Demon
'http://demon.tw
Dim http
Set http = CreateObject("Msxml2.XMLHTTP")
http.open "GET", "https://www.baidu.com", False
http.setRequestHeader "Accept-Encoding", "gzip"
http.send
WScript.Echo http.responseText
從測(cè)試的結(jié)果看,Msxml2.XMLHTTP會(huì)自動(dòng)進(jìn)行GZIP解壓,GOOD!
其次是Msxml2.ServerXMLHTTP:
'By Demon
Dim http
Set http = CreateObject("Msxml2.ServerXMLHTTP")
http.open "GET", "https://www.baidu.com", False
http.setRequestHeader "Accept-Encoding", "gzip"
http.send
WScript.Echo http.responseText
很可惜,返回的是亂碼。再看看WinHttp.WinHttpRequest.5.1:
'By Demon
Dim http
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
http.open "GET", "https://www.baidu.com", False
http.setRequestHeader "Accept-Encoding", "gzip"
http.send
WScript.Echo http.responseText
依然是亂碼。雖然說(shuō)一般情況下用Msxml2.XMLHTTP組件已經(jīng)綽綽有余了,但是有些時(shí)候還是不行的,比如不能發(fā)送Cookie,不能偽造Referer等等。所以還是得想辦法對(duì)GZIP進(jìn)行解碼,辦法無(wú)外乎兩種,自己用VBS寫(xiě)算法或者調(diào)用第三方組件。
算法我就偷懶不寫(xiě)了,感覺(jué)效率不會(huì)太高,哪位朋友感興趣可以寫(xiě)來(lái)玩玩。找了個(gè)不錯(cuò)的第三方組件(居然用第三方,我果然老了)Chilkat.Gzip:
Dim Gzip
Set Gzip = CreateObject("Chilkat.Gzip")
Gzip.UnlockComponent ""
'By Demon
Dim http
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
http.open "GET", "https://www.baidu.com", False
http.setRequestHeader "Accept-Encoding", "gzip"
http.send
WScript.Echo Gzip.UncompressString(http.responseBody, "utf-8")
順便說(shuō)一下這個(gè)組件是收費(fèi)的,可以免費(fèi)試用30天,所以還是應(yīng)該用VBS來(lái)實(shí)現(xiàn)?