XMLHTTP-Request (AJAX) with VBSkript does not work

2019-07-28 04:11发布

问题:

I'm using a XMLHTTP-Request to get data from a website to work with them in VBS. This works great with a default internet connection. But currently I've to deal with a customer, which uses a "proxy" connection.

Unfortunately I'm not very familiar to different proxy-solutions. The behavior is: I'm opening the browser and I'll get a authorisation dialog (username/password). After entering username/password it can be accessed to all websites. If I close the browser and start it again, the autorisation dialog appears again.

Here a little bit of code. I'm working with a "better notepad", so I can't see other object properties:

Set XMLHTTP = CreateObject("Microsoft.XMLHTTP")
XMLHTTP.Open "GET", "http://www.mywebsite.de/getData.php" ,0
XMLHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 
XMLHTTP.Send("content=1")

MsgBox(XMLHTTP.responseText)

If I execute this code on the machine with the forced proxy, I'll get a "access denied" error immediately.

How can I add proxy authorisation support here? If indeed such a thing is possible ...

Thank you!

回答1:

I've found the solution: I have to change the class, because Microsoft.XMLHTTP does not support proxy configurations:

set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.4.0")
xmlhttp.Open "POST","http://www.yourwebsite.com",false
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.setProxy 2, "192.168.0.222:8080"
xmlhttp.setProxyCredentials "your_username" , "password_for_username"
xmlhttp.send

MsgBox(xmlhttp.responseText)

Notice some important details:

set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.4.0")

setProxy and setProxyCredentials methods requires version number: It's important to use a version number at the end of the parameter. If I don't use a version number, it will look for an old version of the class without the setProyx and setProxyCredentials methods. An error message appears ("object does not have the method ...").

Version number isn't trivial: But the version number itself isn't trivial as well. In my local enviroment (W7x64) I'll have to take version number "4.0", but at my client's server it doesn't work ("could not create an object ..."). I'll have to take version number "6.0". If you're not sure, which version is installed, you can look it up in the registry, just search for "MSXML2.ServerXMLHTTP". In my case there are various keys with the right version number: