4

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

3 Answers 3

6

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
Sign up to request clarification or add additional context in comments.

Comments

3

Not sure off hand why you don't get the Location HTTP header, but you can debug by adjusting your code

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

Example output

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

Thinking about it not sure if Location is included unless the server is redirecting from a URL.


Useful Links

Comments

0

If the location isn't provided, it is because the winhttp service isn't registered to the server to connect to. So you can change the user-agent option with objWinhttpReq.Option(0) = "curl" since it works well with curl tool.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.