不少網站為了提高加載速度,啟用HTTP服務器的GZIP壓縮功能,當客戶端發送的HTTP請求中聲明可以接受GZIP編碼時,服務器自動對HTTP響應內容進行GZIP壓縮。但是,在VBS中想自動對GZIP編碼進行解壓就沒有那么容易了。
不同組件對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
從測試的結果看,Msxml2.XMLHTTP會自動進行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
依然是亂碼。雖然說一般情況下用Msxml2.XMLHTTP組件已經綽綽有余了,但是有些時候還是不行的,比如不能發送Cookie,不能偽造Referer等等。所以還是得想辦法對GZIP進行解碼,辦法無外乎兩種,自己用VBS寫算法或者調用第三方組件。
算法我就偷懶不寫了,感覺效率不會太高,哪位朋友感興趣可以寫來玩玩。找了個不錯的第三方組件(居然用第三方,我果然老了)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")
順便說一下這個組件是收費的,可以免費試用30天,所以還是應該用VBS來實現?
原文:http://demon.tw/programming/vbs-http-gzip.html
您可能感興趣的文章:- 在IIS上啟用Gzip壓縮詳細方法(HTTP壓縮)
- 在IIS上啟用Gzip壓縮 (HTTP壓縮)
- PHP中HTTP方式下的Gzip壓縮傳輸方法舉偶
- 關于HTTP傳輸中gzip壓縮的秘密探索分析