使用IE代理设置使用vbscript发送HTTP请求

时间:2015-10-20 12:24:43

标签: internet-explorer web-scraping vbscript proxy httprequest

我需要编写vbscript来向组织拥有的机器发送HTTP请求到远程服务器。最初,我尝试使用MSXML2.ServerXMLHTTP,但看起来有一些代理阻止请求使用脚本。

我可以使用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 个答案:

答案 0 :(得分: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

更新

有一些有用的文章:

Using Fiddler with WinHTTP

WinINet vs. WinHTTP

我想可能有另一种可能的方法来使用没有IE本身的IE代理设置。需要进一步的见解

答案 1 :(得分:0)

使用最新版本的ServerXMLHTTP对象

Set xHttp= CreateObject("MSXML2.ServerXMLHTTP.6.0")
xHttp.Open "POST", SERVER_URL, data, False
xHttp.setProxy 2, "<Your proxy URL>:<PORT>", ""
xHttp.send 
response = xHttp.responseText

msgbox xHttp.status & "|" & xHttp.statustext
msgbox "Response for get call is :" & response

我希望这是你问题的确切答案。 而不是使用InternetExplorer.Applciation

的复杂方法