关于vbscript:使用MSXML2.ServerXMLHTTP.6.0返回的HTTP代码错误

 2021-04-26 

Wrong HTTP code returned using MSXML2.ServerXMLHTTP.6.0

我可以为某些无法正常工作的vbscript代码提供帮助。我正在循环浏览一些Web图像URL,以查看它们是否有效,并发现了一些返回不正确的HTTP返回代码的URL,如下所示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
'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

任何帮助解决这个难题的人都将不胜感激。
-铝


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

相反,请尝试设置一个已知的User-Agent,例如Mozilla Firefox。
另外,您可以使用statusText简化代码。

1
2
3
4
5
6
7
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
User-Agent: Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+5.1)