网络编程
位置:首页>> 网络编程>> Asp编程>> xmlhttp中运行getResponseHeader出错,提示:The requested header was not found

xmlhttp中运行getResponseHeader出错,提示:The requested header was not found

作者:草上飞 来源:草上飞blog 发布时间:2010-03-27 21:47:00 

标签:gzip压缩,getResponseHeader,xmlhttp

在做我的友情链接批量检查工具过程中,碰到一些情况,就是对方网页会用gzip压缩。用gzip压缩的好处是,能压缩网页大小,加快网页的浏览速度,现在越来越多的站长会使用gzip压缩技术以提高网站的浏览速度。

但是这样就会产生一个问题,我们在使用xmlhttp来抓取网页直接抓取的话,抓取到的网页内容将是乱码。

这样,我们则需要在抓取到用gzip压缩的网页后,需要用gzip来解压缩。所有,用gzip和不用gzip获得网页的代码完全不同。

所以我们需要用getResponseHeader("Content-Encoding")来判定所要抓取的网页是否经过了gzip压缩。但是更多的情况是一般网页都不会设置Content-Encoding这个header。则此时我们运行getResponseHeader("Content-Encoding")的时候,将会报错,报错提示如下:The requested header was not found

根据这个提示,我们应该在调用getResponseHeader("Content-Encoding")这个代码前,先判定下抓取的网页是否包含了Content-Encoding这个Header.

则我们可以用如下代码:

If InStr(LCase(http.getAllResponseHeaders()),"content-encoding")>0 then 
  '判定页面返回的Header是否包含Content-Encoding
   content-encoding=http.getResponseHeader("Content-Encoding")
end if

这样就不会报错了。

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com