使用MSXML2.ServerXMLHTTP.6.0返回错误的HTTP代码

时间:2017-02-22 19:50:27

标签: vbscript

我可以使用一些不能按预期工作的vbscript代码。我循环浏览一些网络图片网址,看看他们是否有效并发现一些返回错误的HTTP返回代码,如下所示。

'these urls return code 403 but they are valid image urls
url = "https://www.skymedia.co.uk/wp-content/uploads/2016/05/Sky-Sports-Mix.png"
'url = "https://www.skymedia.ie/wp-content/uploads/sites/4/2016/01/channel-logo-tlc.png"

'this url returns code 403 which is correct
'url = "http://www.lyngsat-logo.com/hires/aa/astro_supersport_my.png"

'this url returns code 200 which is correct
'url = "http://cdn.cablefax.com/wp-content/uploads/2014/06/golf-logo.jpg"

Set req = CreateObject("MSXML2.ServerXMLHTTP.6.0")
req.open "GET", url, False
req.send

'check http status codes
If req.Status = 200 Then httpCode = "HTTP 200 OK - "
If req.Status = 400 Then httpCode = "HTTP 400 Bad Request - "
If req.Status = 403 Then httpCode = "HTTP 403 Forbidden - "
If req.Status = 404 Then httpCode = "HTTP 404 Not Found - "

WScript.Echo httpCode & url

非常感谢任何解决这个谜团的帮助。 -Al

2 个答案:

答案 0 :(得分:1)

某些网络服务器可能拒绝回复某些客户端,如MSXML User-Agent标头用于标识,ServerXMLHTTP的默认值类似于Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5)

相反,尝试设置一个已知的用户代理,例如Mozilla Firefox。 此外,您可以使用statusText来简化代码。

Set req = CreateObject("MSXML2.ServerXMLHTTP.6.0")
req.open "GET", url, False
req.SetRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; rv:51.0) Gecko/20100101 Firefox/51.0"
req.send
httpCode = "HTTP " & req.Status & " " & req.StatusText

WScript.Echo httpCode

答案 1 :(得分:-3)

好的,这个用户代理似乎解决了我的问题:

User-Agent: Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1)