wwHTTP::HttpGetExAsync

Allows you to retrieve HTTP content asynchronously and store the result value into a file. Client code can poll for the result file to retrieve the Async content.

o.HTTPGetExAsync(tcPage,tcResultFile,tnResultSize,tcHeaders)

Return Value

nothing

Parameters

tcPage
Link on the server to load. (/Default.htm)

tcResultFile
Path to a file that will be created with the output from the request.

tnResultSize
The size of the data to be written to the file. This is required, so be sure to specify a size that's large enough. You should be careful not to use too large of a buffer either because memory will be preallocated for this amount of data.

tcHeaders
HTTP Headers to set on the request.

tnSecondsBeforeDelete
Number of seconds for the file to stick around. The Async thread will stick around this long after creating the file and then delete it automatically.

Remarks

The result file controls the lifetime of the async thread as well as the response communication with the calling application. When the file can be opened ( lnHandle=FOPEN(tcFileName,0) with lnHandle being a positive value or File2Var() returning a string ) the thread is done writing the data from the HTTP request, otherwise the file is still locked and the data not complete. Once the file's been written completely the thread will wait for tnSecondsBeforeDelete seconds before erasing the file or until the file is deleted externally by your app. Once either of these events occur the thread is released.

While the thread is active (ie. the file still exists or tnSecondsBeforeDelete is not up) the threads resources are not released. This means if you fail to set tnSecondsBeforeDelete and you don't delete the file your application will continue to add more memory for each async operation you call. If you do this in a loop your application will act as if it has a memory leak. Therefore: Be sure you either delete the file or you set the timeout for only a short period to ensure resources for the async thread are deleted.

If you display the result file or otherwise need to save the the downloaded data it's recommended you copy it to a new location or a string and then delete the original to ensure resources are released and your file is not automatically deleted.

If you quit Visual FoxPro while an async thread is active, VFP will crash!

Example

CLEAR
DO WCONNECT

loIP = CREATEOBJECT("wwHTTP")
loIP.HTTPConnect("www.west-wind.com")

loIP.AddPostkey("FirstName","Rick")
loIP.AddPostkey("Company",TIME())
loIP.AddPostKey("LastName","STRAHL")

lcFile = SYS(2023) + "\AsyncTest.htm"
ERASE (lcFile) 

loIP.HTTPGetExAsync("/wconnect/wc.dll?wwdemo~TestPage",;
                    lcFile,40000,"",10)
loIP.HTTPClose()


*** The following is a simulation of some check code
*** this could be done off a timer or some other processing
*** that took place instead of in this poll loop

lnSeconds = SECONDS()

*** Simulate checking for file
DO WHILE SECONDS() - lnSeconds < 15
  lcResult = File2Var(lcFile)
  ? "Checking...",LEN(lcResult)
  IF EMPTY(lcResult)
     DOEVENTS
     LOOP
  ENDIF

  *** We got our result - let's delete the file
  *** to signal the thread we're done to kill the thread
  ERASE (lcFile)

  EXIT
ENDDO

*** Just display the result as HTML
SHOWHTML(lcResult)

See also:

West Wind Web Connection

© West Wind Technologies, 1996-2018 • Updated: 07/25/09
Comment or report problem with topic