Http Post data slow in WB

Started by Jeremy Whilde, November 11, 2013, 11:07:45 AM

Previous topic - Next topic

Jeremy Whilde

I have been testing a way to post http data to a web host but need fast performance
so this is what I did using VBS.

Dim xmlhttp

Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")

' Page that will receive Post
xmlhttp.Open "POST", "http://posttestserver.com/post.php", False

' Indicate that the body of the request contains form data
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

' Send the data as name/value pairs
xmlhttp.send "Data=test123&more data=456"

' read response body
WScript.Echo xmlhttp.ResponseText

Set xmlhttp = Nothing

Then tried the following in WB

;--------------------------
; UrlEncoded POST form data
;--------------------------

AddExtender("WWINT44I.DLL")
tophandle=iBegin(0,"","")

connecthandle=iHostConnect(tophandle, "posttestserver.com", @HTTP, "", "")

; REQUEST_URI
url1="/post.php"

; Initialise data handle and setup for Http request
datahandle=iHttpInit(connecthandle,"POST",url1,"", 7)
if datahandle==0
   err=iGetLastError()
   Message("Last Error",err)
   iClose(tophandle)
   exit
endif

; Format variable data into encoded URL for POST
urldata=""                             ;initalise url data variable
urldata=iContentUrl(urldata,"data","test123")   ; 1st key data here
urldata=iContentUrl(urldata,"more data","456")   ; 2nd key data etc.

; Actually send the Http data to the host
rslt = iHttpOpen(datahandle, "", urldata, -1);

if rslt=="ERROR" || rslt!=200
   if rslt == "ERROR"
       errstr = "WinInet Error"
       rslt = iGetLastError()
   else
       errstr = "HTTP Error"
   endif
   
   Message(errstr,rslt)
   iClose(tophandle)
   exit
endif

; Check the response from the Http POST
xx=iReadData(datahandle,"test.html")
iClose(datahandle)
iClose(connecthandle)
iClose(tophandle)

; Display response in Notepad
Run("Notepad.exe","test.html")

exit

; Display response in a browser
Origin = DirGet()
test = StrCat(Origin,"test.html")
Run(test,"")
exit

Both VBS & WB methods work but the WB script is considerably slower, is there a better way (faster) in WB to do this?

Thanks JW

stanl

You could try converting the VBS directly to WB. Should run about the same.

Jeremy Whilde

Quote from: stanl on November 11, 2013, 11:35:09 AM
You could try converting the VBS directly to WB. Should run about the same.

Yes that did occur to me after I had posted the examples, maybe I'll give it a go. Was also thinking of maybe running a cURL from WB but that would involve another dependency I guess. Was wondering if anyone else had any slick ways to do this.

Thanks JW

Deana

Here is the vb equivalent WinBatch code:

Code (winbatch) Select
xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")

;Page that will receive Post
xmlhttp.Open("POST", "http://posttestserver.com/post.php", @False)

; Indicate that the body of the request contains form data
xmlhttp.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded")

; Send the data as name/value pairs
xmlhttp.send("Data=test123&more data=456")

; read response body
Pause('ResponseText', xmlhttp.ResponseText)

xmlhttp = 0
exit
Deana F.
Technical Support
Wilson WindowWare Inc.

Deana

 I tested the WinInet code. it seemed quick for me. However I modified the code to use a binary buffer instead of writing out to a file to attempt to speed it up. Here it is:

Code (winbatch) Select

;--------------------------
; UrlEncoded POST form data
;--------------------------

AddExtender("WWINT44I.DLL")
tophandle=iBegin(0,"","")

connecthandle=iHostConnect(tophandle, "posttestserver.com", @HTTP, "", "")

; REQUEST_URI
url1="/post.php"

; Initialise data handle and setup for Http request
datahandle=iHttpInit(connecthandle,"POST",url1,"", 7)
if datahandle==0
   err=iGetLastError()
   Message("Last Error",err)
   iClose(tophandle)
   exit
endif

; Format variable data into encoded URL for POST
urldata=""                             ;initalise url data variable
urldata=iContentUrl(urldata,"data","test123")   ; 1st key data here
urldata=iContentUrl(urldata,"more data","456")   ; 2nd key data etc.

; Actually send the Http data to the host
rslt = iHttpOpen(datahandle, "", urldata, -1);

if rslt=="ERROR" || rslt!=200
   if rslt == "ERROR"
       errstr = "WinInet Error"
       rslt = iGetLastError()
   else
       errstr = "HTTP Error"
   endif
   
   Message(errstr,rslt)
   iClose(tophandle)
   exit
endif

; Check the response from the Http POST
size=10000
buf=BinaryAlloc(size)
;Get address of buffer
bufaddr=IntControl (42, buf, 0, 0, 0)
BinaryEodSet(buf, size)
iReadDataBuf( datahandle, bufaddr, size )
ResponseText = BinaryPeekStr(buf, 0, BinaryEodGet( buf )) 
Pause( "ResponseText", ResponseText )
BinaryFree( buf )
iClose(datahandle)
iClose(connecthandle)
iClose(tophandle)
exit


Deana F.
Technical Support
Wilson WindowWare Inc.

Jeremy Whilde

Deana

Thanks will try both out tomorrow.

Thanks JW

Jeremy Whilde

OK yes both the MSXML2 & the WinInet  with the binary buffer work I still think the MSXML2 is a little faster also tried WinHttpRequest COM object which is also a similar speed.

; Instantiate a WinHttpRequest object.
oWinHttp= CreateObject("WinHttp.WinHttpRequest.5.1")

; Initialize an HTTP request. 
oWinHttp.Open("POST", "http://posttestserver.com/post.php", @false)

; Post data to the HTTP server.
oWinHttp.Send("Data=test123&more data=456")

; read response body
Pause('ResponseText', oWinHttp.ResponseText)

Thanks JW