2017-02-22 86 views
0

我可以使用一些不符合預期的vbscript代碼的幫助。我騎自行車通過幾個網絡圖像網址,看看它們是否有效,並發現一些返回不正確的HTTP返回代碼,如下所示。使用MSXML2.ServerXMLHTTP.6.0返回錯誤的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

*它可能是有益的告訴發生了什麼,以及與預期有什麼不同。 –

回答

1

某些Web服務器可能拒絕響應某些客戶端,如MSXML。
User-Agent標頭用於標識,ServerXMLHTTP的默認類似Mozilla/4.0 (compatible; Win32; WinHttp.WinHttpRequest.5)

而是嘗試設置一個已知的User-Agent,例如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 
+0

謝謝。我給了一個嘗試,但所有的網址現在返回代碼200. –

+0

@ A.Udell我有'403'用於'http:// www.lyngsat-logo.com/hires/aa/astro_supersport_my.png'和'200'對於像你期望的其他人。 –

+0

對不起,應該說得更好。例如,我爲這個URL獲得403,但我可以在我的瀏覽器(IE,Edge)中查看它 - https://www.skymedia.ie/wp-content/uploads/sites/4/2016/01/channel-logo -tlc.png –

-3

好吧,這個用戶代理似乎已經解決了我的問題:「我可以用與預期無法運作的一些VBScript代碼一些幫助」

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

而不是留下另一個答案投票並批准[Kul-Tigin的答案](http://stackoverflow.com/a/42401128/692942),因爲它回答了這個問題。 – Lankymart

相關問題