如何在 VBScript 中获取 http 响应头
How to get http response header in VBScript
为什么这个 .vbs 脚本会检索空值?
Option Explicit
On Error Resume Next
Dim h: Set h = CreateObject("MSXML2.XMLHTTP")
h.Open "HEAD", "http://google.com/", False
h.send
Dim GetLocation: Set GetLocation = h.getResponseHeader("Location")
MsgBox GetLocation
不确定为什么你没有得到 Location
HTTP header,但你可以通过调整代码进行调试
Option Explicit
On Error Resume Next
Dim h: Set h = CreateObject("MSXML2.XMLHTTP")
h.Open "HEAD", "http://google.com/", False
h.send
Dim headers: headers = h.getAllResponseHeaders()
MsgBox headers
示例输出
Date: Fri, 18 Sep 2015 15:33:41 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=UTF-8
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alternate-Protocol: 443:quic,p=1
Alt-Svc: quic=":443"; p="1"; ma=604800
Transfer-Encoding: chunked
考虑不确定是否包含 Location
除非服务器从 URL 重定向。
有用的链接
默认情况下,几乎所有 HTTP 库都遵循重定向。
因此,只要您遵循重定向,就无法获得 Location
header,因此您需要停止遵循重定向。
我推荐两种不同的解决方案。
#1 实现最终的 url 将是最简单的方法,而不是获得 Location
header.
Option Explicit
Dim GetLocation
Const SXH_OPTION_URL = -1
Dim h
Set h = CreateObject("MSXML2.ServerXMLHTTP")
h.Open "HEAD", "http://google.com/", False
h.send
GetLocation = h.getOption(SXH_OPTION_URL) 'final URL even if redirected
MsgBox GetLocation
#2 如果您想确保获得 第一个 Location
header(不是最后 link 在重定向链中),您应该通过禁用重定向来使用 WinHttpRequest,这样您就可以获得 header(如果可用)。
Option Explicit
Dim GetLocation
Const WHR_EnableRedirects = 6
Dim h
Set h = CreateObject("WinHttp.WinHttpRequest.5.1")
h.Option(WHR_EnableRedirects) = False 'disable redirects
h.Open "HEAD", "http://google.com/", False
h.Send
GetLocation = h.GetResponseHeader("Location") 'an error occurs if not exist
MsgBox GetLocation
如果没有提供位置,那是因为 winhttp 服务没有注册到要连接的服务器。因此,您可以使用 objWinhttpReq.Option(0) = "curl"
更改 user-agent 选项,因为它适用于 curl 工具。
Option Explicit
Dim GetLocation
Const WHR_UserAgent = 0
Dim h
Set h = CreateObject("WinHttp.WinHttpRequest.5.1")
h.Option(WHR_UserAgent ) = "curl"
h.Open "HEAD", "http://google.com/", False
h.Send
GetLocation = h.GetResponseHeader("Location") 'an error occurs if not exist
MsgBox GetLocation
为什么这个 .vbs 脚本会检索空值?
Option Explicit
On Error Resume Next
Dim h: Set h = CreateObject("MSXML2.XMLHTTP")
h.Open "HEAD", "http://google.com/", False
h.send
Dim GetLocation: Set GetLocation = h.getResponseHeader("Location")
MsgBox GetLocation
不确定为什么你没有得到 Location
HTTP header,但你可以通过调整代码进行调试
Option Explicit
On Error Resume Next
Dim h: Set h = CreateObject("MSXML2.XMLHTTP")
h.Open "HEAD", "http://google.com/", False
h.send
Dim headers: headers = h.getAllResponseHeaders()
MsgBox headers
示例输出
Date: Fri, 18 Sep 2015 15:33:41 GMT Expires: -1 Cache-Control: private, max-age=0 Content-Type: text/html; charset=UTF-8 Server: gws X-XSS-Protection: 1; mode=block X-Frame-Options: SAMEORIGIN Alternate-Protocol: 443:quic,p=1 Alt-Svc: quic=":443"; p="1"; ma=604800 Transfer-Encoding: chunked
考虑不确定是否包含 Location
除非服务器从 URL 重定向。
有用的链接
默认情况下,几乎所有 HTTP 库都遵循重定向。
因此,只要您遵循重定向,就无法获得 Location
header,因此您需要停止遵循重定向。
我推荐两种不同的解决方案。
#1 实现最终的 url 将是最简单的方法,而不是获得 Location
header.
Option Explicit
Dim GetLocation
Const SXH_OPTION_URL = -1
Dim h
Set h = CreateObject("MSXML2.ServerXMLHTTP")
h.Open "HEAD", "http://google.com/", False
h.send
GetLocation = h.getOption(SXH_OPTION_URL) 'final URL even if redirected
MsgBox GetLocation
#2 如果您想确保获得 第一个 Location
header(不是最后 link 在重定向链中),您应该通过禁用重定向来使用 WinHttpRequest,这样您就可以获得 header(如果可用)。
Option Explicit
Dim GetLocation
Const WHR_EnableRedirects = 6
Dim h
Set h = CreateObject("WinHttp.WinHttpRequest.5.1")
h.Option(WHR_EnableRedirects) = False 'disable redirects
h.Open "HEAD", "http://google.com/", False
h.Send
GetLocation = h.GetResponseHeader("Location") 'an error occurs if not exist
MsgBox GetLocation
如果没有提供位置,那是因为 winhttp 服务没有注册到要连接的服务器。因此,您可以使用 objWinhttpReq.Option(0) = "curl"
更改 user-agent 选项,因为它适用于 curl 工具。
Option Explicit
Dim GetLocation
Const WHR_UserAgent = 0
Dim h
Set h = CreateObject("WinHttp.WinHttpRequest.5.1")
h.Option(WHR_UserAgent ) = "curl"
h.Open "HEAD", "http://google.com/", False
h.Send
GetLocation = h.GetResponseHeader("Location") 'an error occurs if not exist
MsgBox GetLocation