如何在VBScript中获取http响应头文件 [英] How to get http response header in VBScript
问题描述
为什么这个.vbs脚本检索到空值?
选项显式
错误恢复下一个
Dim h:Set h = CreateObject(MSXML2.XMLHTTP)
h.OpenHEAD,http://google.com/,False
h.send
Dim GetLocation :Set GetLocation = h.getResponseHeader(Location)
MsgBox GetLocation
几乎所有的HTTP库都默认遵循重定向。
因此,只要您遵循以下步骤,您就无法获得 Location
$ b
#1 strong>实现最终的url将是最简单的方式,而不是获取 Location
标题。
Option Explicit
Dim GetLocation
Const SXH_OPTION_URL = -1
Dim h
Set h = CreateObject(MSXML2.ServerXMLHTTP)
h。打开HEAD,http://google.com/,False
h.send
GetLocation = h.getOption(SXH_OPTION_URL)'最终URL,即使重定向
MsgBox GetLocation
#2 如果您想确保先获得 位置
标题(而不是重定向链中的最后一个链接),则应使用 WinHttpRequest 通过禁用重定向,所以你可以得到标题,如果可用的话。
Option Explicit
Dim GetLocation
Const WHR_EnableRedirects = 6
Dim h
Set h = CreateObject(WinHttp.WinHttpRequest.5.1 )
h.Option(WHR_EnableRedirects)= False'禁用重定向
h.OpenHEAD,http://google.com/,False
h.Send
GetLocation = h.GetResponseHeader(Location)如果不存在,则会发生错误
MsgBox GetLocation
Why this .vbs script retrieve null value?
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
Almost all HTTP libraries follow redirects by default.
Therefore you can not get Location
header as long as you follow the redirects, so you need to stop following redirects.
I recommend two different solutions.
#1 Achieving the final url would be the easiest way instead of getting 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 If you want to make sure you get the first Location
header (not the last link in the chain of redirects), you should use WinHttpRequest by disabling redirects, so you can get the header if available.
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
这篇关于如何在VBScript中获取http响应头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!