wwHTTP::OnHttpBufferUpdate

This class can be used to make HTTP requests to Web servers, to retrieve data. You can also send data to the server using POST or PUT operations. All HTTP verbs are supported.

At it's simplest you can retrieve HTTP content like this:

loHttp = CREATEOBJECT("wwHttp")
lcHtml = loHttp.Get("https://markdownmonster.west-wind.com")

Content retrieved can also be saved to a file directly:

loHttp = CREATEOBJECT('wwhttp')
loHttp.Get("https://west-wind.com/files/wwclient.zip","","",;
        "c:\temp\wwclient.zip")

The wwHttp class provides HTTP access via WinInet to Visual FoxPro application. This class provides tight control over most aspects of the HTTP protocol including HTTP headers (client and server side), timeouts, proxy support, authentication, SSL connectivity as well as providing support for asynchronous HTTP operation.

For a variety of examples of using this class please see:

Based on: Relation
Stored in: wwHTTP.prg

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-2019 • Updated: 02/15/19
Comment or report problem with topic