Use IE proxy settings to send HTTP request using v

2019-05-19 07:07发布

问题:

I need to write vbscript to send HTTP request to remote server for a machine owned by an organization. Initially, I tried with MSXML2.ServerXMLHTTP but looks like there is some proxy blocking requests made using the script.

I can send requests using Internet Explorer just fine, so IE has proxy settings configured.

My script looks like this now:

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

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

Is there any way to get proxy settings from IE and use it in vbscript somehow? I can't find any reference on internet about particular issue.

回答1:

There is a possible workaround, you can try to use IE intrinsic 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

UPDATE

There are some useful articles:

Using Fiddler with WinHTTP

WinINet vs. WinHTTP

I guess there might be another possible way to use IE proxy settings without IE itself. Need further insight



回答2:

Use the latest version of ServerXMLHTTP object

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

I hope this is the exact answer to your question. Instead of using the complicated approach of InternetExplorer.Applciation