wwHTTP::OnHttpBufferUpdate

This method can be used to display progress information on Http downloads. It gets called whenever the receive buffer is updated on an HTTPGet update.

There are two ways you can handle buffer updates:

  • Use BINDEVENT to hook OnHttpBufferUpdate
    By using BINDEVENT you can route the event handling to any object of you choice including to a form that you're running in (see example).

  • Subclass wwHttp with a custom class
    By subclassing wwHttp you can override the OnHttpBufferUpdate method of the wwHttp class and so provide the default implementation that fires on every buffer update

The method receives a lnBufferReads parameter which determines what type of request is being made:

  • 0 - HTTP Header is sent
  • -1 - The HTTP request was completed or cancelled. (lHttpCancelDownload is set if cancelled)

Any other value greater than 0 is the number of times OnHTTPBufferUpdate was called in which case you can check the other parameters for the current download size and current chunk of data.

o.onhttpbufferupdate(lnBytesDownloaded,lnBufferReads,lcCurrentChunk, loHttp)

Parameters

lnBytesDownloaded
Number of bytes that have been downloaded so far.

lnBufferReads
Number of reads that have occurred. The following special values can be passed in here: 0 - HTTP Header for the request -1 - Request was completed or cancelled. See lHttpCancelDownload.

lcCurrentChunk
The current block of bytes read from the Request.

**loHttp **
wwHttp instance for this buffer update. Useful if you're using BINDEVENT to route the event to another object rather than subclassing. For subclasses you can also use the THIS. pointer instead to reference the FTP object.

Remarks

Be careful with what kind of code you put into the 'event' method since it fires on every buffer update. Keep code in this method and any called methods to a minimum to avoid download slowdowns.

If you want things like Cancel buttons you will need to use DOEVENTS in the event code to allow the click events to fire. even so the responsiveness of the button may not be very good due to VFP's limited concurrent processing capabilities.

We also suggest that you increase your buffer size so that this event doesn't fire so frequently. Increasing nHTTPWorkBufferSize on the Http instance can significantly improve the performance of HTTP operations.

Example

Using Subclassing
DO wwHTTP
loHttp = CREATEOBJECT("wwHttp_WithEvents")

*** Make the HTTP call - lcHTML is set but output is not used
*** the output written by this example is written from the
*** event handler as data is retrieved
lcHTML = loHttp.HttpGet("http://www.microsoft.com")

* create sub-class of wwHttp and implement OnHttpBufferUpdate
DEFINE CLASS wwHttp_WithEvents as wwHttp

FUNCTION OnHttpBufferUpdate
LPARAMETERS lnbytes,lnbufferreads,lccurrentchunk, loHttp

DO CASE
    *** 0 - Http Header
	CASE lnBufferReads = 0
		lcHttpHeader = lcCurrentChunk
    *** -1 - done or cancelled or failed
	CASE lnBufferReads = -1
		* done or cancelled 		
		IF THIS.lhttpcanceldownload 
			? "Cancelled"
		ELSE
			? "---"
			? "Completed..."
		ENDIF
	*** anything else - data has been read
	CASE lnBufferReads > 0
	    WAIT WINDOW TRANSFORM(lnBytes,"9,999,999") + " of " + TRANSFORM(THIS.nContentSize,"9,999,999") + " bytes" NOWAIT 
ENDCASE

*** You can also accumuluate the data incrementally
*** to allow users to look at data as it comes in
? lcCurrentChunk

ENDDEFINE
Using BINDEVENT
DO wwHTTP
loHttp = CREATEOBJECT("wwHttp")

*** Object that will handle the event. This could be a form 
*** or any other object instance on which you implement
*** the OnHttpBufferUpdate event handler code
loSink = CREATEOBJECT("HttpEventSink")

*** Bind event from wwHttp to the event handling object
BINDEVENT(loHTTP,"OnHttpBufferUpdate",loSink,"OnHttpBufferUpdate")

*** Make the HTTP call - lcHTML is set but output is not used
*** the output written by this example is written from the
*** event handler as data is retrieved
lcHTML = loHttp.HttpGet("http://www.microsoft.com")

RETURN

*** If you have a class instance - a Form for example
*** you can use that class as the Event sink and simply
*** add the method below to it
DEFINE CLASS HttpEventSink as Custom

FUNCTION OnHttpBufferUpdate
LPARAMETERS lnbytes,lnbufferreads,lccurrentchunk, loHttp

DO CASE
    *** 0 - Http Header
	CASE lnBufferReads = 0
		lcHttpHeader = lcCurrentChunk
    *** -1 - done or cancelled or failed
	CASE lnBufferReads = -1
		* done or cancelled 		
		IF loHttp.lhttpcanceldownload 
			? "Cancelled"
		ELSE
			? "---"
			? "Completed..."
		ENDIF
	*** anything else - data has been read
	CASE lnBufferReads > 0
	    WAIT WINDOW TRANSFORM(lnBytes,"9,999,999") + " of " + TRANSFORM(loHTTP.nContentSize,"9,999,999") + " bytes" NOWAIT 
ENDCASE

*** Dump data incrementally
? lcCurrentChunk

ENDDEFINE

See also:

West Wind Web Connection | wwHTTP::lHttpCancelDownload | wwHTTP::HTTPCanceldownload

© West Wind Technologies, 1996-2018 • Updated: 11/29/16
Comment or report problem with topic