Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTTPPost + Lag

Status
Not open for further replies.

ScopeXL

Programmer
Aug 26, 2010
2
US
Hello,

New to the forums. This looks like a place I can get some answers. I am passing a URL through my VBScript with a HTTPPost function, which works great, except when the server (website) lags, it also lags the software in which this script is run. Basically I am trying to get my script to pass a URL to the server without causing lag to the client. I am assuming this has to be done asyncronously, in which case I have hit a roadblock. Any help is appreciated. Thank you.

My script/function:

sUrl = "sRequest = "u=" & UserID & "&p=" & Password & "&f=11&c=clr all"
HTTPPost sUrl, sRequest

Function HTTPPost(sUrl, sRequest)
set oHTTP = CreateObject("Microsoft.XMLHTTP")
oHTTP.open "Get", sUrl & "?" & sRequest ,false
oHTTP.setRequestHeader "Content-Type", "application/x- oHTTP.send
HTTPPost = oHTTP.responseText
End Function
 
[0] To do it asynchronous in script, you have to make up a onreadystatechange handler and register it with the oHTTP.

[1] And then you have to keep the script alive in case the ohttp has not finished its work before other script lines not depending on the response has finished.

[2] The way HTTPPost is called, the return is not captured in any way outside the function. It is in that sense not correct, but, of course, you can do everything exhaustively with the function. Except it is the latter case, you have to change the script on that point.

[3] This is how you do it in the big plan.
[tt]
[blue]dim gbdone, gresp, oHTTP
gbdone=false
gresp=""
set oHTTP=nothing[/blue]

sUrl = "[ignore][/ignore]"
sRequest = "u=" & UserID & "&p=" & Password & "&f=11&c=clr all"
HTTPPost sUrl, sRequest

[blue]'continue to do other things which does not need gsmsg[/blue]

[blue]'keep the script alive in case oHTTP is not done yet
do while not gbdone
wscript.sleep 500
loop

'do further thing if needed... (if still need gresp, do not reset it to empty in the on_rsc_handler.[/blue]

Function HTTPPost(sUrl, sRequest)
set oHTTP = CreateObject("Microsoft.XMLHTTP")
[blue]if sRequest<>"" then
oHTTP.open "Get", sUrl & "?" & sRequest,[red]true[/red]
else
oHTTP.open "Get",sUrl,[red]true[/red]
end if

'reset/initialize global
gbdone=false
gresp=""[/blue]

'this is not strictly needed as no form data is posted
oHTTP.setRequestHeader "Content-Type", "application/x-
[blue]'register your onreadystatechange handler
oHTTP.onreadystatechange=getref("on_rsc_handler")[/blue]
oHTTP.send

'let free the function
[red]'[/red]HTTPPost = oHTTP.responseText
End Function

[blue]function on_rsc_handler
if not (oHTTP is nothing) and not gbdone then
if oHTTP.readystate=4 then
'set/reset the global variables
gbdone=true
gresp=oHTTP.responseText
set oHTTP=nothing
'do every thing depending on gresp in a function, say, do_responseText
do_responseText gresp
'gresp="" 'reset it to empty (optional, depending on need)
end if
end if
end function

function do_responseText(s)
'do everything you need depending on the responseText here
end function[/blue]
[/tt]
 
Works like a charm. Thank you. One issue is the client that this is being run on doesn't utilize the Windows Scripting Host. Instead they use the ActiveX control (msscript.ocx). Is there an alternative to the WScript.Sleep 500?
 
[4] To make a sleep-like script, there are sure many possible "solution" limited by people's imagination. But then, people criticize this or that aspect as clumsy, non-generic... whatever. A built-in sleep has a big virtue, being that it shuts everybody's mouth up (even the binary implementation of it is very bad - nobody is aware of it.)

[4.1] You can use some component that has some kind of sleep exposed through automation interface.

[4.2] You can write your own (actually not really difficult) component and instantiate it...

[4.3] You can write some dirty mathematics killing time...

[4.4] I would be practical hereafter rather than parading! Can you not use a synthetic sleep? (sleeping period would have to be near to bigger than 1 sec minimum.) Like this.
[tt]
'keep the script alive in case oHTTP is not done yet
dim wshshell
set wshshell=createobject("wscript.shell")
do while not gbdone
wshshell.run "ping -n 2 127.0.0.1>nul",0,true
loop
set wshshell=nothing
[/tt]
[4.5] I can suggest a more complicated approach to some eye. A cmd window open synchronous is listening eternally. Make it hidden to avoid user clicking close. And then when the oHTTP finishes the work, write a win32_process instance to kill the cmd window characterised by special opening commandline to facilitate its identification. (But then, some process may need sufficient privileges of the user... hence, it is not for "Everyone"... But now, the sleep is once and for all.

[4.5.1]
[tt]
'keep the script alive in case oHTTP is not done yet
dim gscmd, gsparam, wshshell
gscmd="cmd.exe"
gsparam="/k date/t" 'some special call to make cmdline special
set wshshell=createobject("wscript.shell")
if not gbdone then
wshshell.run gscmd & " " & gsparam,0,true
end if
[/tt]
[4.5.2] And now the callback should do something more, namely to kill the listening cmd window.
[tt]
function on_rsc_handler
if not (oHTTP is nothing) and not gbdone then
if oHTTP.readystate=4 then
'set/reset the global variables
gbdone=true
gresp=oHTTP.responseText
set oHTTP=nothing
'do every thing depending on gresp in a function, say, do_responseText
do_responseText gresp
'gresp="" 'reset it to empty (optional, depending on need)
[blue]set svc=getobject("winmgmts:{(Debug)}!root\cimv2")
squery="select name, commandline from win32_process where commandline like '%" & gscmd & "%" & gsparam & "%'"
set colproc=svc.execquery(squery)
for each oproc in colproc
nret=oproc.terminate() 'can control nret
next
set colproc=nothing
set svc=nothing[/blue]
end if
end if
end function
[/tt]
[4.5.3] The testing and debugging (including my typos etc) of it may well be more complicated, hence, it may not be suitable for self-confessed beginners?! In any case, Caveat emptor.
 
[5] I have now a chance to look at your query string. It needs to be encoded if you have spaces or some escapable character in it.
>sRequest = "u=" & UserID & "&p=" & Password & "&f=11&c=clr all"
[tt]sRequest = "u=" & UserID & "&p=" & Password & "&f=11&c=clr[red]%20[/red]all"[/tt]

[5.1] And you still need to be careful about password in the clear... (as well as their need of the same encoding.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top