当前位置: 代码网 > it编程>前端脚本>其它脚本 > VBS获取GZIP压缩的HTTP内容的实现代码

VBS获取GZIP压缩的HTTP内容的实现代码

2024年05月19日 其它脚本 我要评论
不少网站为了提高加载速度,启用http服务器的gzip压缩功能,当客户端发送的http请求中声明可以接受gzip编码时,服务器自动对http响应内容进行gzip压缩。但是,在vbs中想自动对gzip编

不少网站为了提高加载速度,启用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

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com