0

I'm trying to use WinHTTP in JScript (ES3) to a-synchronously retrieve the responsetext of an internal site that requites SSO log-on. The below works (I've replaced the url with "http://www.google.com/"), but I don't know how to get a callback or check when WinHTTP is done. I cannot use XMLHTTP as that won't log on automatically and stays on readystate 1, and it seems that WinHTTP doesn't support 'onreadystatechange'. As I need to use ES3/JScript, I cannot use JQuery or other plug-ins and need it to be 'vanilla'.

Here's what I have now.

var a = '';
var x = new ActiveXObject('WinHTTP.WinHTTPRequest.5.1'), method = 'GET', url = 'http://www.google.com/';"
    x.SetAutoLogonPolicy(0);
    x.open(method, url, true);
    x.send();

So this succesfully returns the request, but I don't know how to trigger it so that on completion it will put the responsetext into variable 'a'. When using XMLHTTP it would work like below, but as mentioned, that won't work with my SSO site:

Working for google, but not for my internal site - and the 'onreadystatechange' doesn't work with winhttp...

var a = '';
var x = new ActiveXObject('Microsoft.XMLHTTP'), method = 'GET', url = 'https://www.google.com/';
    x.open(method, url, true);")
    x.onreadystatechange = function () {if(x.readyState === 4 && x.status === 200){a = x.responsetext;}};
    x.send();

I would appreciate guidance! Thank you

JasperD
  • 152
  • 1
  • 3
  • 15
  • What do you mean by "*as that won't log on automatically and stays on readystate 1*"? – Bergi Sep 19 '18 at 11:06
  • Google found me this: https://learn.microsoft.com/en-us/windows/desktop/winhttp/retrieving-data-using-script - looks like there are no callbacks like you're used to, it's entirely synchronous. But possibly multithreaded - ouch. – Bergi Sep 19 '18 at 11:28
  • @Bergi - if I use my actual link instead of "http://www.google.com", the readystate will be '1' (loaded) and never flip to '4' (ready). – JasperD Sep 19 '18 at 15:24
  • @Bergi I've seen WinHTTP asynchronous in VBA here: https://stagesolutions.wordpress.com/2012/04/18/asynchronous-download-from-http-with-vba/ but 'onResponseFinished' doesn't work in javascript - trying to find a callback for that. – JasperD Sep 19 '18 at 15:39
  • I would recommend to keep trying with `XMLHTTP` and investigate what exactly the request consists of and why the server doesn't respond. – Bergi Sep 19 '18 at 18:05
  • @Bergi - it is due to XMLHTTP's problems with CORS; this seems to be useful though: https://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/ - I haven't fully read it yet, nor know if I could implement, but I'll definitely try. – JasperD Sep 19 '18 at 22:27
  • @JasperD May I ask in which environment will this script work? HTA, WSH, MSIE, ScriptControl, ASP (phew!) ? – Kul-Tigin Sep 26 '18 at 07:40
  • @Kul-Tigin: ScriptControl – JasperD Sep 26 '18 at 16:25

0 Answers0