北漂IT民工 的博客

获取HTTP请求的服务器响应头的方法

我们有很多种可以不用自己写程序得到HTTP响应头的办法。

下面是我常用到的办法:

1.通过浏览器插件得到HTTP头:

a) firefox的插件 firebug

b) chrome 开发人员工具

2.Windows下面的小工具,HTTPDebug

http://emouze.com/httpdebug.htm

3.Linux 下面的wget

wget功能很强大,得到响应头的方式

wget -S –spider [server domain] #得到响应头并自动重定向

wget -S –spider -E –max-redirect 0 [server domain] #得到响应头但不自动重定向

示例:

1)

eric@eric-laptop:~/Documents/python$ wget -S –spider -E –max-redirect 0 www.google.com

Spider mode enabled. Check if remote file exists.

–2010-04-08 13:20:00– http://www.google.com/

正在解析主机 www.google.com… 64.233.189.104

正在连接 www.google.com|64.233.189.104|:80… 已连接。

已发出 HTTP 请求,正在等待回应…

HTTP/1.1 302 Found

Via: 1.1 GOOGLE-ISA

Connection: Keep-Alive

Proxy-Connection: Keep-Alive

Content-Length: 445

Date: Thu, 08 Apr 2010 05:20:00 GMT

Location: http://www.google.com.hk/url?sa=p&cki=PREF%3DID%3D2319aa0b1214c31d:FF%3D2:LD%3Dzh-CN:NW%3D1:TM%3D1270704000:LM%3D1270704000:S%3D79hIXkdlD8VAV97u&q=http://www.google.com.hk/&ust=1270704030555252&usg=AFQjCNHCeuxtlXUpuAEHWO-OEgdkz0rAhg

Content-Type: text/html; charset=UTF-8

Server: gws

Cache-Control: private

Set-Cookie: PREF=ID=2319aa0b1214c31d:NW=1:TM=1270704000:LM=1270704000:S=_UJ9V-1ckhKWoD7b; expires=Sat, 07-Apr-2012 05:20:00 GMT; path=/; domain=.google.com

Set-Cookie: NID=33=GulJPSHv80-fIemwCRTBzkwTjZphsfhO2Xe1L4txr890PbNeb2KiUmmYi5UL83Cb_Ffo7DGumKV3FO-k-tvBq-HaP8FaH79zzwXr8hSCVPJnBwp-0W3nry7UmtbtmvTv; expires=Fri, 08-Oct-2010 05:20:00 GMT; path=/; domain=.google.com; HttpOnly

位置:http://www.google.com.hk/url?sa=p&cki=PREF%3DID%3D2319aa0b1214c31d:FF%3D2:LD%3Dzh-CN:NW%3D1:TM%3D1270704000:LM%3D1270704000:S%3D79hIXkdlD8VAV97u&q=http://www.google.com.hk/&ust=1270704030555252&usg=AFQjCNHCeuxtlXUpuAEHWO-OEgdkz0rAhg [跟随至新的 URL]

已超过 0 次重定向。


2)

eric@eric-laptop:~/Documents/python$ wget -S –spider www.google.com

Spider mode enabled. Check if remote file exists.

–2010-04-08 13:21:13– http://www.google.com/

正在解析主机 www.google.com… 64.233.189.104

正在连接 www.google.com|64.233.189.104|:80… 已连接。

已发出 HTTP 请求,正在等待回应…

HTTP/1.1 302 Found

Via: 1.1 GOOGLE-ISA

Connection: Keep-Alive

Proxy-Connection: Keep-Alive

Content-Length: 445

Date: Thu, 08 Apr 2010 05:21:13 GMT

Location: http://www.google.com.hk/url?sa=p&cki=PREF%3DID%3Df78beea3e41bc59e:FF%3D2:LD%3Dzh-CN:NW%3D1:TM%3D1270704073:LM%3D1270704073:S%3DiiZR2XqZVdFUI-bo&q=http://www.google.com.hk/&ust=1270704103279594&usg=AFQjCNH-eCwSn9dDanA4duv5NFE09uh-HQ

Content-Type: text/html; charset=UTF-8

Server: gws

Cache-Control: private

Set-Cookie: PREF=ID=f78beea3e41bc59e:NW=1:TM=1270704073:LM=1270704073:S=Dh7dpIOjBsgRYVDQ; expires=Sat, 07-Apr-2012 05:21:13 GMT; path=/; domain=.google.com

Set-Cookie: NID=33=CJVX8tBCrfovzaVc1zKEakjAlUjOnOnuYZC5bBLE4KqLLEzybsuEpizVdy4iKiOTxqXmsRMa2sV6f_wRCAJ6Gtgihy8qCVng-Z56Ei3B7B9fOVVAByafIBr5u7d-yTLP; expires=Fri, 08-Oct-2010 05:21:13 GMT; path=/; domain=.google.com; HttpOnly

位置:http://www.google.com.hk/url?sa=p&cki=PREF%3DID%3Df78beea3e41bc59e:FF%3D2:LD%3Dzh-CN:NW%3D1:TM%3D1270704073:LM%3D1270704073:S%3DiiZR2XqZVdFUI-bo&q=http://www.google.com.hk/&ust=1270704103279594&usg=AFQjCNH-eCwSn9dDanA4duv5NFE09uh-HQ [跟随至新的 URL]

Spider mode enabled. Check if remote file exists.

–2010-04-08 13:21:13– http://www.google.com.hk/url?sa=p&cki=PREF%3DID%3Df78beea3e41bc59e:FF%3D2:LD%3Dzh-CN:NW%3D1:TM%3D1270704073:LM%3D1270704073:S%3DiiZR2XqZVdFUI-bo&q=http://www.google.com.hk/&ust=1270704103279594&usg=AFQjCNH-eCwSn9dDanA4duv5NFE09uh-HQ

正在解析主机 www.google.com.hk… 64.233.189.104

正在连接 www.google.com.hk|64.233.189.104|:80… 已连接。

已发出 HTTP 请求,正在等待回应…

HTTP/1.1 302 Found

Via: 1.1 GOOGLE-ISA

Connection: Keep-Alive

Proxy-Connection: Keep-Alive

Content-Length: 222

Date: Thu, 08 Apr 2010 05:21:13 GMT

Location: http://www.google.com.hk/

Content-Type: text/html; charset=UTF-8

Server: gws

Cache-Control: private

Set-Cookie: PREF=ID=f78beea3e41bc59e:FF=2:LD=zh-CN:NW=1:TM=1270704073:LM=1270704073:S=iiZR2XqZVdFUI-bo; expires=Sat, 07-Apr-2012 05:21:13 GMT; path=/; domain=.google.com.hk

位置:http://www.google.com.hk/ [跟随至新的 URL]

Spider mode enabled. Check if remote file exists.

–2010-04-08 13:21:13– http://www.google.com.hk/

正在连接 www.google.com.hk|64.233.189.104|:80… 已连接。

已发出 HTTP 请求,正在等待回应…

HTTP/1.1 200 OK

Via: 1.1 GOOGLE-ISA

Connection: close

Proxy-Connection: close

Expires: -1

Date: Thu, 08 Apr 2010 05:21:13 GMT

Content-Type: text/html; charset=GB2312

Server: gws

Cache-Control: private, max-age=0

Set-Cookie: NID=33=AqGQjsAr_jks5PM94Ubo-jhh5rZvK8QPhSgb40PglSXeynlaIpoqWf7Fr-cCc0eUkc-7MzQM9fE2Als2LDwI6mSe4S5aVJoE49NmgtpOzsF9Le7xUB3PJIJ4n0Ce2-ND; expires=Fri, 08-Oct-2010 05:21:13 GMT; path=/; domain=.google.com.hk; HttpOnly

长度: 未指定 [text/html]

Remote file exists and could contain further links,

but recursion is disabled – not retrieving.


可以看到如果没有禁止重定向次数,对google的请求将会得到很多次HTTP头