2015-10-20 85 views
2

我需要編寫vbscript來發送HTTP請求到組織擁有的機器的遠程服務器。最初,我嘗試了MSXML2.ServerXMLHTTP,但看起來有一些代理阻塞請求使用腳本。使用IE代理設置使用vbscript發送HTTP請求

我可以使用Internet Explorer發送請求就好了,所以IE配置了代理設置。

我的腳本現在看起來是這樣的:

Set xHttp = CreateObject("Microsoft.XMLHTTP") 
'Set http = CreateObject("MSXML2.XMLHTTP") 

xHttp.Open "POST", SERVER_URL, data, False 
xHttp.Send 

有沒有辦法從IE獲得代理設置,並在VBScript中以某種方式使用它?我無法在網上找到關於特定問題的任何參考。

回答

2

有一種可能的解決方法,你可以嘗試使用IE內在XHR:

With CreateObject("InternetExplorer.Application") 
    .Visible = True ' debug only 
    .Navigate "https://www.google.com/" ' navigate to the same domain where the target file located 
    Do While .ReadyState <> 4 Or .Busy 
     wscript.Sleep 10 
    Loop 
    arrLocationURL = Split(.LocationURL, "/") 
    strLocationURL = arrLocationURL(0) & "//" & arrLocationURL(2) & "/" ' .com might be changed to certain Country code top-level domain 
    With .document.parentWindow 
     .execScript "var xhr = new XMLHttpRequest", "javascript" ' create XHR instance 
     With .xhr 
      .Open "GET", strLocationURL & "images/branding/googlelogo/2x/googlelogo_color_272x92dp.png", False ' open get request 
      .Send 
      arrContent = .responseBody ' retrieve binary content 
     End With 
    End With 
    .Quit 
End With 

With CreateObject("Adodb.Stream") 
    .Type = 1 
    .Open 
    .Write arrContent ' put content to the stream 
    .SaveToFile CreateObject("WScript.Shell").SpecialFolders.Item(&H0) & "\googlelogo.png", 2 ' save .png file to desktop 
    .Close 
End With